Home > Software engineering >  How to loop through many columns
How to loop through many columns

Time:09-01

I have about 88 columns in a pandas dataframe. I'm trying to apply a formula that calculates a single value for each column. How do I switch out the name of each column and then build a new single-row dataframe from the equation?

Below is the equation (linear mixed model) which results in a single value for each column.

B1 = (((gdf.groupby(['Benthic_Mo'])['SHAPE_Area'].sum())/Area_sum) * 
  (gdf.groupby(['Benthic_Mo'])['W8_629044'].mean())).sum()

Below is a sample of the names of the columns

['OBJECTID', 'Benthic_Mo', 'SHAPE_Leng', 'SHAPE_Area', 'geometry', 'tmp', 'Species','W8_629044', 'W8_642938', 'W8_656877', 'W8_670861', 'W8_684891', 'W8_698965', 'W8_713086', 'W8_72726',...]

The columns with W8_## need to be switched out in the formula, but about 80 of them are there. The output I need is a new dataframe with a single row. I also would like to calculate the variance or Standard deviation from the data calculated with the formal.

thank you!

CodePudding user response:

You can loop through the dataframe columns. I think the below code should work.

collist = list(orignal_dataframe.columns)
emptylist = []
for i in collist[7:]:
    B1 = (((gdf.groupby(['Benthic_Mo'])['SHAPE_Area'].sum())/Area_sum) *  (gdf.groupby(['Benthic_Mo'])[i].mean())).sum()
    res  = {i:B1}
    emptylist.append(res)
resdf = pd.DataFrame(emptydict)

CodePudding user response:

to create new df with the results in each new col (one row), you can use similar as below:

W8_cols = [col for col in df.columns if 'W8_' in col]

df_out = pd.DataFrame()
for col in W8_cols:
    B1 = (((gdf.groupby(['Benthic_Mo'])['SHAPE_Area'].sum()) / Area_sum) *
          (gdf.groupby(['Benthic_Mo'])['W8_629044'].mean())).sum()
    t_data = [{col: B1}]
    df_temp = pd.DataFrame(t_data)
    data = [df_out, df_temp]
    df_out = pd.concat(data, axis=1)
  • Related