I'm trying to add a column for density to a dataframe, which is calculated by the equation shown. I need the reference density to change at a certain row (row 66). I've tried two for loops and a for/else loop and they each use one of the densities for every row, rather than switching as I intend. I have defined all the variables, and am not getting any errors.
Attempt #1
for index, row in tem80df[tem80df['DREF'] <= 66].iterrows():
density80 = density_crust * (1-(alpha * tem80df['TREF']))
for index, row in tem80df[tem80df['DREF'] > 66].iterrows():
density80= density_mantle * (1- (alpha *tem80df['TREF']))
Attempt #2
for index, row in tem80df[tem80df['DREF'] <= 66].iterrows():
density80 = density_crust * (1-(alpha * tem80df['TREF']))
else:
density80 = density_mantle * (1 - (alpha* tem80df['TREF']))
'DREF' and 'TREF' are column names. 'DREF' is the first column.
Help would be appreciated!
question from:
https://stackoverflow.com/questions/65938346/different-equation-applied-to-specific-rows-in-dataframe 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…