【发布时间】:2014-08-28 10:34:07
【问题描述】:
这可能非常简单,但我就是找不到答案。我使用 GeoPandas 从形状文件导入数据。把它变成熊猫DataFrame。我有一个包含三个字母代码和None 缺失数据值的对象字段。如何在熊猫中将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'
不会改变任何东西。
【问题讨论】:
-
你是指字符串'None'还是空字符串?
-
您可以使用
sala['N10'].isnull()测试空值,因此您可以执行 sala.loc[sala['N10'].isnull(), 'N10'] = 'vcv'`应该可以工作 -
@Ajean:我认为您的回答
sala.fillna(value='vcv')有效。为什么要删除? -
是的,它是“无”。 Ajean:我只需要该列来更改表中的所有内容,而不是像您建议的那样。我有很多数字,所以 vcv 是不行的。我正在合并一个文本字段,它不适用于 None。
-
那么
sala.loc[sala['N10'] == 'None', 'N10'] = 'vcv'有效吗?