'How do I replace the list of nan with list of zeroes in a pandas dataframe cell
I have this dataframe. How do I replace the list of nan with list of zeroes?
a b
1 [nan, nan]
5 [nan, nan, nan]
0 [0, 0]
Solution 1:[1]
One option is explode + fillna + groupby:
df['b'] = df['b'].explode().fillna(0).groupby(level=0).agg(list)
Another option is list comprehension:
df['b'] = [[0 if isinstance(x, float) and np.isnan(x) else x for x in lst] for lst in df['b']]
Output:
a b
0 1 [0, 0]
1 5 [0, 0, 0]
2 0 [0, 0]
Solution 2:[2]
You can also use fillna by temporarily converting to Series:
df['b'] = df['b'].apply(lambda x: pd.Series(x).fillna(0).to_list())
NB. This is probably not efficient on large datasets. The list comprehension is very likely the fastest.
Solution 3:[3]
How about a list comprehension?
df['b'] = [[0 if pd.isna(y) else y for y in x] for x in df['b']]
df
a b
0 1 [0, 0]
1 5 [0, 0, 0]
2 0 [0, 0]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 |
