Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 pandas.replace删除第一个值,但不删除';不要加第二个_Python_Pandas_Replace - Fatal编程技术网

Python pandas.replace删除第一个值,但不删除';不要加第二个

Python pandas.replace删除第一个值,但不删除';不要加第二个,python,pandas,replace,Python,Pandas,Replace,我试图将.csv列中的数据:“1(1789-1790)”替换为一年:“1789” 我首先读了.csv: df = pd.read_csv('congress1.csv', delimiter = ';', names = ['Name', 'Years', 'Position', 'Party', 'State', 'Congress'], header = 0) 然后我对列='Congress'中的数据运行.replace,并为其提供新字符串: df['Congress'] = df['Co

我试图将.csv列中的数据:“1(1789-1790)”替换为一年:“1789”

我首先读了.csv:

df = pd.read_csv('congress1.csv', delimiter = ';', names = ['Name', 'Years', 'Position', 'Party', 'State', 'Congress'], header = 0)
然后我对列='Congress'中的数据运行.replace,并为其提供新字符串:

df['Congress'] = df['Congress'].replace('1(1789-1790)', '1789', inplace=True)

但是,当我打印数据时,该列显示“无”。

当您使用
inplace=True
时,
replace
方法返回
None
(然后重新分配给该列)并就地修改对象

演示:

s
已修改到位,
res
None


您只需省略
inplace=True
部分,或者不重新分配给
df['Congress']

谢谢。我不知道inplace就是这么做的。然而,现在数据保持不变。我将更新原始问题以反映。@CharlieGoldberg好吧,现在这几乎是一个新问题。我已经回答了为什么会有一个列用
None
填充,但是如果没有看到数据帧,我无法回答您的新问题。我建议你打开一个新问题,而不是编辑这个问题。请确保包含一个可以让其他人完全重现问题的解决方案。
>>> s = pd.Series([1,2,3])
>>> s
>>> 
0    1
1    2
2    3
dtype: int64
>>> 
>>> res = s.replace(1, 2, inplace=True)
>>> res is None
>>> True
>>> s
>>> 
0    2
1    2
2    3
dtype: int64