I have the following dictionary:
fillna(value={'first_name':'Andrii', 'last_name':'Furmanets', 'created_at':None})
When I pass that dictionary to fillna
I see:
raise ValueError('must specify a fill method or value')\nValueError: must specify a fill method or value\n"
It seems to me that it fails on None
value.
I use pandas version 0.20.3.
In case you want to normalize all of the nulls with python's None. The first fillna will replace all of (None, NAT, np. nan, etc) with Numpy's NaN, then replace Numpy's NaN with python's None. Very useful when using xlwings, which doesn't support NA s, but supports None s.
In Pandas missing data is represented by two value: None: None is a Python singleton object that is often used for missing data in Python code. NaN : NaN (an acronym for Not a Number), is a special floating-point value recognized by all systems that use the standard IEEE floating-point representation.
pandas fillna NaN with None Value In order to update the existing DataFrame use df. fillna('None', inplace=True) . You can also use pandas. DataFrame.
Definition and UsageThe fillna() method replaces the NULL values with a specified value. The fillna() method returns a new DataFrame object unless the inplace parameter is set to True , in that case the fillna() method does the replacing in the original DataFrame instead.
In case you want to normalize all of the nulls with python's None.
df.fillna(np.nan).replace([np.nan], [None])
The first fillna
will replace all of (None, NAT, np.nan, etc) with Numpy's NaN, then replace Numpy's NaN with python's None.
Setup
Consider the sample dataframe df
df = pd.DataFrame(dict(A=[1, None], B=[None, 2], C=[None, 'D'])) df A B C 0 1.0 NaN None 1 NaN 2.0 D
I can confirm the error
df.fillna(dict(A=1, B=None, C=4))
ValueError: must specify a fill method or value
This happens because pandas is cycling through keys in the dictionary and executing a fillna
for each relevant column. If you look at the signature of the pd.Series.fillna
method
Series.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
You'll see the default value is None
. So we can replicate this error with
df.A.fillna(None)
Or equivalently
df.A.fillna()
I'll add that I'm not terribly surprised considering that you are attempting to fill a null value with a null value.
What you need is a work around
Solution
Use pd.DataFrame.fillna
over columns that you want to fill with non-null values. Then follow that up with a pd.DataFrame.replace
on the specific columns you want to swap one null value with another.
df.fillna(dict(A=1, C=2)).replace(dict(B={np.nan: None})) A B C 0 1.0 None 2 1 1.0 2 D
What type of data structure are you using? This works for a pandas Series:
import pandas as pd
d = pd.Series({'first_name': 'Andrii', 'last_name':'Furmanets', 'created_at':None})
d = d.fillna('DATE')
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