在 Pandas 中重命名“无”值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24619145/
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-09-13 22:14:16  来源:igfitidea点击:

Rename "None" value in Pandas

pythonpandasnullrename

提问by user1246428

This is probably super simple but I just can not find the answer. I import data using GeoPandas from a shape file. Turn that into pandas DataFrame. I have a object field with three letter codes and Nonevalues for missing data. How do I change None's to something like "vcv" in pandas? I tried this

这可能非常简单,但我找不到答案。我使用 GeoPandas 从形状文件导入数据。把它变成 pandas DataFrame。我有一个包含三个字母代码和None缺失数据值的对象字段。如何None在Pandas中将 's更改为“vcv”之类的内容?我试过这个

sala.replace(None,"vcv")

got this error

得到这个错误

   2400                                     "strings or regular expressions, you "
   2401                                     "passed a"
-> 2402                                     " {0!r}".format(type(regex).__name__))
   2403                 return self.replace(regex, value, inplace=inplace, limit=limit,
   2404                                     regex=True)

TypeError: 'regex' must be a string or a compiled regular expression or a list or dict of strings or regular expressions, you passed a 'bool'

Tried this

试过这个

if sala['N10'] is None:
    sala['N10'] = 'Nul'

Does not change anything.

不会改变任何东西。

回答by EdChum

The simplest thing to do is just:

最简单的事情就是:

sala['N10'].replace('None', 'vcv', inplace=True)

that should work.

那应该工作。

If they were true NaNvalues then calling fillnawould've worked.

如果它们是真NaN值,那么调用fillna就会起作用。

e.g.

例如

sala['N10'].fillna(value='vcv', inplace = True)

also what I suggested in my comment:

还有我在评论中提出的建议:

sala.loc[sala['N10'].isnull(), 'N10'] = 'vcv'