我正在尝试创建一个逐行遍历 pandas 数据框的函数。我想根据其他列的行值创建一个新列。我的原始数据框可能如下所示:
df:
A B
0 1 2
1 3 4
2 2 2
现在我想创建一个新列,在每个索引位置填充列 A - 列 B 的行值,以便结果如下所示:
df:
A B A-B
0 1 2 -1
1 3 4 -1
2 2 2 0
我的解决方案有效,但仅当我不在函数中使用它时:
for index, row in df.iterrows():
print index
df['A-B']=df['A']-df['B']
这为我提供了所需的输出,但是当我尝试将其用作函数时,出现错误。
def test(x):
for index, row in df.iterrows():
print index
df['A-B']=df['A']-df['B']
return df
df.apply(test)
ValueError: cannot copy sequence with size 4 to array axis with dimension 3
我在这里做错了什么,我怎样才能让它发挥作用?
最佳答案
是因为apply
默认情况下方法适用于列,如果你想通过行将 axis
更改为 1:
axis : {0 or ‘index’, 1 or ‘columns’}, default 0
- 0 or ‘index’: apply function to each column
- 1 or ‘columns’: apply function to each row
df.apply(test, axis=1)
编辑
我认为您需要对每一行进行一些复杂的操作。如果您只需要相互减去列:
df['A-B'] = df.A - df.B
关于python - 如何在遍历 pandas 数据框时创建新列并插入行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34128984/