I have a dataframe with a column where all its elements are lists but some elements are NaN. Something like this:
Date | Value |
---|---|
01/01/2022 | 0, 16 |
02/01/2022 | 0, 22 |
03/01/2022 | 0, 15 |
04/01/2022 | 0, 2 |
05/01/2022 | NaN |
I'm trying to separate the values in two others columns, one for each data of the list with the pandas function to_list. But I can't do it works having NaN in the column. I could do dropna but I need the date data. My intention is to replace NaN with 0, 0.
At the end what I want to achieve is this result, no matter how to get there:
Date | Value | A | B |
---|---|---|---|
01/01/2022 | 0, 16 | 0 | 16 |
02/01/2022 | 4, 22 | 4 | 22 |
03/01/2022 | 8, 15 | 8 | 15 |
04/01/2022 | 8, 2 | 8 | 2 |
05/01/2022 | NaN | 0 | 0 |
Thanks!!
CodePudding user response:
In your case just do split
out = df.join(df['Value'].str.split(', ',expand=True).fillna(0))
#df['Value'].str.split(', ',expand=True).fillna(0)
Out[34]:
0 1
0 0 16
1 0 22
2 0 15
3 0 2
4 0 0
CodePudding user response:
Assuming your Value
column is in fact type list
, not str
, you could:
df[["A", "B"]] = df["Value"].apply(pd.Series).fillna(0)