pandas 从同一数据帧中减去 2 行 [英] Pandas subtract 2 rows from same dataframe
本文介绍了 pandas 从同一数据帧中减去 2 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在以下数据帧 (df) 中从另一行中减去一行:
How do I subtract one row from another in the following dataframe (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'
推荐答案
您可以使用 df.loc
来按索引值选择行:
You can select rows by index value using 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
这篇关于 pandas 从同一数据帧中减去 2 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文