我的df如下所示:
Index Country Val1 Val2 ... Val10 1 Australia 1 3 ... 5 2 Bambua 12 33 ... 56 3 Tambua 14 34 ... 58
我想从每个国家的Val1中减去Val10,所以输出如下:
Country Val10-Val1 Australia 4 Bambua 23 Tambua 24
到目前为止,我已经:
def myDelta(row): data = row[['Val10', 'Val1']] return pd.Series({'Delta': np.subtract(data)}) def runDeltas(): myDF = getDF() \ .apply(myDelta, axis=1) \ .sort_values(by=['Delta'], ascending=False) return myDF
runDeltas导致此错误:
ValueError: ('invalid number of arguments', u'occurred at index 9')
解决此问题的正确方法是什么?
给定以下数据框:
df = pd.DataFrame([["Australia", 1, 3, 5], ["Bambua", 12, 33, 56], ["Tambua", 14, 34, 58] ], columns=["Country", "Val1", "Val2", "Val10"] )
它归结为一个简单的广播操作):
>>> val1_minus_val10 = df["Val1"] - df["Val10"] >>> print(val1_minus_val10) 0 -4 1 -44 2 -44 dtype: int64