pandas 数据框任意两列之间的百分比差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42100058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Percentage difference between any two columns of pandas dataframe
提问by user1124702
I would like to have a function defined for percentage diff calculation between any two pandas columns. Lets say that my dataframe is defined by:
我想为任意两个 Pandas 列之间的百分比差异计算定义一个函数。假设我的数据框是由以下定义的:
R1 R2 R3 R4 R5 R6
A B 1 2 3 4
I would like my calculation defined as
我希望我的计算定义为
df['R7'] = df[['R3','R4']].apply( method call to calculate perc diff)
and
和
df['R8'] = df[['R5','R6']].apply(same method call to calculate perc diff)
How to do?
怎么做?
I have tried below
我在下面试过
df['perc_cnco_error'] = df[['CumNetChargeOffs_x','CumNetChargeOffs_y']].apply(lambda x,y: percCalc(x,y))
def percCalc(x,y):
if x<1e-9:
return 0
else:
return (y - x)*100/x
and it gives me the error message
它给了我错误信息
TypeError: ('() takes exactly 2 arguments (1 given)', u'occurred at index CumNetChargeOffs_x')
类型错误: ('() 正好需要 2 个参数 (1 给定)', u'occurred at index CumNetChargeOffs_x')
回答by pdubucq
This would give you the deviation in percentage:
这会给你百分比偏差:
df.apply(lambda row: (row.iloc[0]-row.iloc[1])/row.iloc[0]*100, axis=1)
If you have more than two columns try,
如果您有两列以上的尝试,
df[['R3', 'R5']].apply(lambda row: (row.iloc[0]-row.iloc[1])/row.iloc[0]*100, axis=1)