如何在以下数据框(df)中从另一行中减去一行:
RECL_LCC 1 2 3
RECL_LCC 35.107655 36.015210 28.877135
RECL_PI 36.961519 43.499506 19.538975
我想做类似的事情:
df['Difference'] = df['RECL_LCC']-df['RECL_PI']
但是这给了:
*** KeyError: 'RECL_LCC'
答案 0 :(得分:4)
您可以使用df.loc
按
In [98]: df.loc['Diff'] = df.loc['RECL_LCC'] - df.loc['RECL_PI']
In [99]: df
Out[99]:
RECL_LCC 1 2 3
RECL_LCC 35.107655 36.015210 28.877135
RECL_PI 36.961519 43.499506 19.538975
Diff -1.853864 -7.484296 9.338160
答案 1 :(得分:0)