这可能非常简单,但我就是找不到答案。我使用 GeoPandas 从形状文件导入数据。将其转换为 pandas DataFrame
。我有一个包含三个字母代码和 None
缺失数据值的对象字段。如何在 pandas 中将 None
更改为类似“vcv”的内容?我试过了
sala.replace(None,"vcv")
遇到这个错误
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'
试过了
if sala['N10'] is None:
sala['N10'] = 'Nul'
不改变任何东西。
最佳答案
最简单的事情就是:
sala['N10'].replace('None', 'vcv', inplace=True)
应该可以。
如果它们是真正的 NaN
值,那么调用 fillna
就可以了。
例如
sala['N10'].fillna(value='vcv', inplace = True)
还有我在评论中的建议:
sala.loc[sala['N10'].isnull(), 'N10'] = 'vcv'
关于python - 在 Pandas 中重命名 "None"值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24619145/