Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename "None" value in Pandas

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.

like image 526
user1246428 Avatar asked Jul 07 '14 20:07

user1246428


People also ask

How do I change none value in Python?

Use a list comprehension to replace None values in a list in Python, e.g. new_list_1 = ['' if i is None else i for i in my_list] . The list comprehension should return a different value, e.g. an empty string or 0 if the list item is None , otherwise it should return the list item. Copied!

How do you replace None value?

You can use DataFrame. fillna or Series. fillna which will replace the Python object None , not the string 'None' .


1 Answers

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'
like image 131
EdChum Avatar answered Oct 06 '22 22:10

EdChum