Home > Blockchain >  Dividing each column in a pandas df by a value from another df
Dividing each column in a pandas df by a value from another df

Time:08-03

I have a dataframe of a size (44,44) and another one (44,)enter image description here

enter image description here

I need to divide each item in a column 'EOFx' by a number in a column 'PCx'. (e.g. All values in 'EOF1' by 'PC1') I've been trying string and numeric loops but nothing seems to work at all (error) or I get NaNs. Last thing I tried was

for k in eof_df.keys():
    for m in pc_df.keys():
        eof_df[k].divide(pc_df[m])

The end result is a modified eof_df. What did work for 1 column outside the loop is this.

eof_df.iloc[:,0].divide(std_df.iloc[0]).head()

Thank you!

upd1. In response to MoRe: for eof_df it will be:

{'EOF1': {'8410140.nc': -0.09481700372712784,
  '8418150.nc': -0.11842440098461708,
  '8443970.nc': -0.1275311990493338,
  '8447930.nc': -0.1321116945944401,
  '8449130.nc': -0.11649753033608201,
  '8452660.nc': -0.14776686151828214,
  '8454000.nc': -0.1451132595405897,
  '8461490.nc': -0.17032364516557338,
  '8467150.nc': -0.20725618455428937,
  '8518750.nc': -0.2249648853806308},
 'EOF2': {'8410140.nc': 0.051213689088367806,
  '8418150.nc': 0.0858110390036938,
  '8443970.nc': 0.09029173023479754,
  '8447930.nc': 0.05526955432871537,
  '8449130.nc': 0.05136680082838883,
  '8452660.nc': 0.06105351220962777,
  '8454000.nc': 0.052112043784544135,
  '8461490.nc': 0.08652511173850089,
  '8467150.nc': 0.1137754089944319,
  '8518750.nc': 0.10461193696203},

and it goes to EOF44. For pc_df it will be

{'PC1': 0.5734671652560537,
 'PC2': 0.29256502033278076,
 'PC3': 0.23586098119374838,
 'PC4': 0.227069130368915,
 'PC5': 0.1642170373016029,
 'PC6': 0.14131097046499339,
 'PC7': 0.09837935104899741,
 'PC8': 0.0869056762311067,
 'PC9': 0.08183389338415169,
 'PC10': 0.07467191608481094}

CodePudding user response:

output = pd.DataFrame(index=eof_df.index, data=eof_df.values / pc_df.values)
output.columns = eof_df.columns

CodePudding user response:

data = pd.DataFrame(eof_df.values.T / pc_df.values.T).T
data.columns = ["divided"   str(i   1) for i in data.columns.to_list()]
  • Related