how to replace zero-dimensional arrays in pandas dataframe?

Viewed 200

I have a pandas dataframe that looks like this:

index array array2
1 ['group1'] ['group1, 'group3']
2 ['group1', 'group4] ['group1', 'group3']
3 [] ['group2', 'group3']
4 [] ['group2', 'group4']

as you can see, some of these arrays are zero-dimensional (to be specific: they are

array([], dtype=object

)

Now, because zero-dimensional arrays can not be concatenated, I want to replace them as np.nans so that I can concatenate them.

but if I do

data['array'].replace(np.array([]).astype(object), 0, inplace= True)

Nothing happens! The dataframe stays the same and nothing changes. In fact, even if I do it manually:

data['array'].replace(data['array'][3], 0, inplace = True)

my resulting dataframe is not altered at all...

My question then is, how can we create a function to replace all of these zero-dimensional arrays in a data frame for concatenation?

3 Answers

You can try:

data['array'] = [x if len(x) else 0 for x in data['array']]

You can access its length for masking:

>>> df.loc[df.array.str.len().eq(0), "array"] = np.nan
>>> df

   index             array            array2
0      1          [group1]  [group1, group3]
1      2  [group1, group4]  [group1, group3]
2      3               NaN  [group2, group3]
3      4               NaN  [group2, group4]

You can also use mask:

data = data.mask(data.applymap(str).eq('[]'))

Or you can use this:

data['array'] = data['array'].where(data['array'].str.len() > 2, np.nan)

   index             array            array2
0      1          [group1]  [group1, group3]
1      2  [group1, group4]  [group1, group3]
2      3               NaN  [group2, group3]
3      4               NaN  [group2, group4]
Related