Python 熊猫:从一列中进行简单的数字减法
我需要简单地从数据帧中的一列中减去2。df中有更多的列需要保持不变 以下内容无效。我做错了什么Python 熊猫:从一列中进行简单的数字减法,python,pandas,Python,Pandas,我需要简单地从数据帧中的一列中减去2。df中有更多的列需要保持不变 以下内容无效。我做错了什么 func = lambda x: x - 2 df["customer"].apply(func) apply() In [49]: df Out[49]: a b c 0 2 9 1 1 0 5 9 2 1 5 6 3 6 3 4 4 8 0 8 In [50]: df["a"].apply(func) Out[50]: 0 0 1 -2 2
func = lambda x: x - 2
df["customer"].apply(func)
apply()
In [49]: df
Out[49]:
a b c
0 2 9 1
1 0 5 9
2 1 5 6
3 6 3 4
4 8 0 8
In [50]: df["a"].apply(func)
Out[50]:
0 0
1 -2
2 -1
3 4
4 6
Name: a, dtype: int64
In [51]: df
Out[51]:
a b c
0 2 9 1
1 0 5 9
2 1 5 6
3 6 3 4
4 8 0 8
您想要的是:df[“customer”]-=2
,它将工作得更快,看起来更好,而且更地道
In [53]: df['a'] -= 2
In [54]: df
Out[54]:
a b c
0 0 9 1
1 -2 5 9
2 -1 5 6
3 4 3 4
4 6 0 8