2

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?

1
  • Experimenting with replace, it looks like it can match strings and numbers, but doesn't seem to work with list or array. Your array has shape (0,), so it is actually 1d. Doing an equality test on such array is tricky (or on any array). Also beware that the list [], and string "[] display the same as your array. Commented Apr 6, 2021 at 4:06

3 Answers 3

1

You can try:

data['array'] = [x if len(x) else 0 for x in data['array']]
Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.