Python 熊猫替换元素不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37593550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:36:37  来源:igfitidea点击:

Pandas replacing elements not working

pythonnumpypandasreplace

提问by dter

I have looked up this issue and most questions are for more complex replacements. However in my case I have a very simple dataframe as a test dummy.

我查过这个问题,大多数问题都是针对更复杂的替换。但是,在我的情况下,我有一个非常简单的数据框作为测试假人。

The aim is to replace a string anywhere in the dataframe with an nan, however this does not seem to work (i.e. does not replace; no errors whatsoever). I've tried replacing with another string and it does not work either. E.g.

目的是用 nan 替换数据帧中任何位置的字符串,但这似乎不起作用(即不替换;没有任何错误)。我试过用另一个字符串替换,它也不起作用。例如

d = {'color' : pd.Series(['white', 'blue', 'orange']),
   'second_color': pd.Series(['white', 'black', 'blue']),
   'value' : pd.Series([1., 2., 3.])}
df = pd.DataFrame(d)
df.replace('white', np.nan)

The output is still:

输出仍然是:

      color second_color  value
  0   white        white      1
  1    blue        black      2
  2  orange         blue      3

回答by user1761806

Given that this is the top Google result when searching for "Pandas replace is not working" I'd like to also mention that:

鉴于这是搜索“Pandas replace is not working”时的最高谷歌结果,我还想提一下:

replace does full replacement searches, unless you turn on the regex switch. Use regex=True, and it should perform partial replacements as well.

replace 执行完全替换搜索,除非您打开正则表达式开关。使用 regex=True,它也应该执行部分替换。

This took me 30 minutes to find out, so hopefully I've saved the next person 30 minutes.

这花了我 30 分钟才找到答案,所以希望我已经为下一个人节省了 30 分钟。

回答by EdChum

You need to assign back

你需要重新分配

df = df.replace('white', np.nan)

or pass param inplace=True:

或传递参数inplace=True

In [50]:
d = {'color' : pd.Series(['white', 'blue', 'orange']),
   'second_color': pd.Series(['white', 'black', 'blue']),
   'value' : pd.Series([1., 2., 3.])}
df = pd.DataFrame(d)
df.replace('white', np.nan, inplace=True)
df

Out[50]:
    color second_color  value
0     NaN          NaN    1.0
1    blue        black    2.0
2  orange         blue    3.0

Most pandas ops return a copy and most have param inplacewhich is usually defaulted to False

大多数 Pandas 操作返回一个副本,大多数都有参数inplace,通常默认为False

回答by ysearka

When you use df.replace()it creates a new temporary object, but doesn't modify yours. You can use one of the two following lines to modify df:

当您使用df.replace()它时会创建一个新的临时对象,但不会修改您的。您可以使用以下两行之一来修改 df:

df = df.replace('white', np.nan)
df.replace('white', np.nan, inplace = True)

回答by Daniil Mashkin

Neither one with inplace=Truenor the other with regex=Truedon't work in my case. So I found a solution with using Series.str.replaceinstead. It can be useful if you need to replace a substring.

在我的情况下,一个 withinplace=True或另一个 withregex=True都不起作用。所以我找到了一个使用Series.str.replace的解决方案。如果您需要替换子字符串,它会很有用。

In [4]: df['color'] = df.color.str.replace('e', 'E!')
In [5]: df  
Out[5]: 
     color second_color  value
0   whitE!        white    1.0
1    bluE!        black    2.0
2  orangE!         blue    3.0

or even with a slicing.

甚至切片。

In [10]: df.loc[df.color=='blue', 'color'] = df.color.str.replace('e', 'E!')
In [11]: df  
Out[11]: 
    color second_color  value
0   white        white    1.0
1   bluE!        black    2.0
2  orange         blue    3.0