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.
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!
You can use DataFrame. fillna or Series. fillna which will replace the Python object None , not the string 'None' .
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With