0

I have a pandas DataFrame with a combination of numpy.ndarray and categorical variables.

import pandas as pd
import numpy as np

df = pd.DataFrame({
                   'i': [0, 1, 2],
                   'a': [np.array([]), np.array([]), 'A']
                 })

df['a'][0]

array([], dtype=float64)

I'd like to replace the arrays with np.nan.

I tried: df['a'].replace(np.array([]), np.nan) but it didn't work.

1
  • Do you want to replace empty arrays only or all arrays? Commented Jul 31, 2022 at 20:47

2 Answers 2

2

If you want to replace empty arrays by nan, you can convert the column a as boolean mask:

df['a'] = df['a'].where(df['a'].astype(bool), np.nan)
print(df)

# Output
   i    a
0  0  NaN
1  1  NaN
2  2    A
Sign up to request clarification or add additional context in comments.

Comments

1

One way could be checking with isinstance:

# on one column
df['a'] = df['a'].apply(lambda x: np.nan if isinstance(x, np.ndarray) else x)

#whole df
df = df.applymap(lambda x: np.nan if isinstance(x, np.ndarray) else x)

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.