Rename “None” value in Pandas

2019-04-06 13:52发布

问题:

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'