Issue
I have a Pandas DataFrame with a series containing dicts
.
import pandas as pd
df = pd.DataFrame({
'c': [np.nan, 'N', np.nan, 'N'],
'd': [{'c':'C'}, np.nan, {'c':'C'}, np.nan]
})
I am attempting to fillna
with:
df.fillna(np.where(df['d'].str['c'] == "C", "Y", "N"), inplace=True)
Traceback:
/Applications/Anaconda/anaconda3/lib/python3.9/site-packages/pandas/core/generic.py in fillna(self, value, method, axis, inplace, limit, downcast)
6501 elif isinstance(value, ABCDataFrame) and self.ndim == 2:
6502
-> 6503 new_data = self.where(self.notna(), value)._mgr
6504 else:
6505 raise ValueError(f"invalid fill value with a {type(value)}")
ValueError: invalid fill value with a <class 'numpy.ndarray'>
Solution
From the doc
This value cannot be a list.
out = df.fillna({'c':(df['d'].str['c'] == "C").map({True: "Y", False: "N"})})
Out[68]:
c d
0 Y {'c': 'C'}
1 N NaN
2 Y {'c': 'C'}
3 N NaN
Answered By - BENY
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.