jeudi 4 juin 2020

How to combine df.loc with for-loop to calculate new columns in pandas

I would like to learn how to use df.loc and for-loop to calculate new columns for the dataframe below Problem: from df_G, for T = 400, take value of each Go_j as input Then add new column "G_ads_400" in dataframe df = df['Adsorption_energy_eV'] - Go_h2o here is my code for each Temperature

Go_co2 = df_G.loc[df_G.index == "400" & df_G.Go_CO2]
Go_o2= df_G.loc[df_G.index == "400" & df_G.Go_O2]
Go_co= df_G.loc[df_G.index == "400" & df_G.Go_CO]

df.loc[df['Adsorbates'] == "CO2", "G_ads_400"] = df.Adsorption_energy_eV-Go_co2
df.loc[df['Adsorbates'] == "CO", "G_ads_400"] = df.Adsorption_energy_eV-Go_co
df.loc[df['Adsorbates'] == "O2", "G_ads_400"] = df.Adsorption_energy_eV-Go_o2

I am not sure why I kept having error and I would like to know how to put it in a for-loop so it looks less messy

df_G

dataframe df

Aucun commentaire:

Enregistrer un commentaire