0

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'>

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a way to do what I believe you're trying for without using fillna():

df.c[df.c.isna()] = np.where(df.d.str['c'] == 'C', 'Y', 'N')

Output:

   c           d
0  Y  {'c': 'C'}
1  N         NaN
2  Y  {'c': 'C'}
3  N         NaN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.