请问
python里的自定义函数怎么应用到dataframe里呀
比如我已经读取了一个excel ,定义为df1
我想用自定义函数实现A列减B列得到一列数
应该怎么实现呀

dataframe怎么作为函数参数被传递到自定义函数里
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
关注
进行减法运算,A-B 添加到了第E列
代码如下:import pandas as pd def func_test(a, b): return a - b df1 = pd.read_excel('01.xlsx') print(df1) result = func_test(df1['A'], df1['B']) df1['E'] = result.values print(df1)
输出为:
A B C D 0 1 10 19 28 1 2 11 20 29 2 3 12 21 30 3 4 13 22 31 A B C D E 0 1 10 19 28 -9 1 2 11 20 29 -9 2 3 12 21 30 -9 3 4 13 22 31 -9 Process finished with exit code 0
如有问题及时沟通
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用