python - 如何在遍历 pandas 数据框时创建新列并插入行值

标签 python pandas iteration dataframe

我正在尝试创建一个逐行遍历 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/

相关文章:

python - 每月重新采样数据帧并将每月的计数存储在新数据帧中,其中列为日期和计数

c++ - 在列表迭代期间删除元素 - 安全

python - 为列表中的项目生成一个新线程

python - 检查十六进制值列表是否连续

python - 从函数添加 driver.get() 值时,Selenium 参数无效

python - 限制 xticks pandas 条形图中的字符

python - 找到两个地理数据点之间的交集

Python 数据框串联

python - 返回非实例化类类型的类型提示

python - Zabbix Server 的时间戳格式