Rename “None” value in Pandas

坚强是说给别人听的谎言 提交于 2019-12-12 08:41:59

问题


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 None values for missing data. How do I change None's to something like "vcv" in pandas? I tried this

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.


回答1:


The simplest thing to do is just:

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

that should work.

If they were true NaN values then calling fillna would've worked.

e.g.

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

also what I suggested in my comment:

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


来源:https://stackoverflow.com/questions/24619145/rename-none-value-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!