Home > database >  Replace numpy arrays in pandas series
Replace numpy arrays in pandas series

Time:08-01

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.

CodePudding user response:

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)

CodePudding user response:

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
  • Related