Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 熊猫:从一列中进行简单的数字减法_Python_Pandas - Fatal编程技术网

Python 熊猫:从一列中进行简单的数字减法

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

我需要简单地从数据帧中的一列中减去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   -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