Dataframes Calculate Multiple Columns Based one other Column – for Loop

Question:

I’m working with multiple data frames and I am trying to perform a change the values of columns 3-9 to their value divided by the value of the third column which contains a population. I could run it manually on a single column, but would rather not do it for every column in each data frame, so I am hoping to find a for loop so I can run it on each data frame which has the same columns for each year. The columns I am trying to change are all int64, and the one I am using as the divisor is a float64.

I was thinking something like :

for column in df.iloc[:, 3:10]:
column.value() = column.value()/ df[‘Population’]

So I can change the data frame name and run it on each, or even better I can create a function which takes the argument of the name of the data frame, but I was just hoping for this to get started. Thanks!

Asked By: Amy Johnson

||

Answers:

IIUC, you can create a list of dataframes:

dfs_list = [df, df1, df3, df4] # specify your DFs here

Then iterating over the columns using a forloop:

def update_columns(x):
    for col in x.columns[3:10]:
        x[col] = x[col] / x['Population']

And apply the func to each df in the list:

for df in dfs_list:
    update_columns(df)
Answered By: dramarama