在Pandas中重命名"None"值

use*_*428 8 python null rename pandas

这可能超级简单,但我找不到答案.我从形状文件中使用GeoPandas导入数据.把它变成熊猫DataFrame.我有一个对象字段,其中包含三个字母代码和None缺失数据的值.如何None在熊猫中将其更改为"vcv"?我试过这个

sala.replace(None,"vcv")
Run Code Online (Sandbox Code Playgroud)

得到了这个错误

   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'
Run Code Online (Sandbox Code Playgroud)

试过这个

if sala['N10'] is None:
    sala['N10'] = 'Nul'
Run Code Online (Sandbox Code Playgroud)

什么都不改变.

EdC*_*ica 12

最简单的事情就是:

sala['N10'].replace('None', 'vcv', inplace=True)
Run Code Online (Sandbox Code Playgroud)

应该工作.

如果他们是真正的NaN价值观,那么打电话fillna就会奏效.

例如

sala['N10'].fillna(value='vcv', inplace = True)
Run Code Online (Sandbox Code Playgroud)

也是我在评论中建议的:

sala.loc[sala['N10'].isnull(), 'N10'] = 'vcv'
Run Code Online (Sandbox Code Playgroud)

  • 好的,小小姐沟通,它是“无”,因为没有任何值,但在打印时却显示“无”。但它不是文本“无”。您的解决方案 sala.loc[sala['N10'].isnull(), 'N10'] = 'vcv' 效果很好。谢谢,马修 (2认同)