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?
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.