I have a pandas dataframe like so:
df = pd.DataFrame({'column': [[np.nan, np.nan, np.nan], [1, np.nan, np.nan], [2, 3, np.nan], [3, 2, 1]]})
column
0 [nan, nan, nan]
1 [1, nan, nan]
2 [2, 3, nan]
3 [3, 2, 1]
Note that there is never the same value twice in a row.
I wish to transform this single column into multiple columns named with the corresponding values. So I want to order the values and put them in the right column. The ones under column_1
, twos under column_2
etc.
column_1 column_2 column_3
0 NaN NaN NaN
1 1.0 NaN NaN
2 NaN 2.0 3.0
3 1.0 2.0 3.0
How to do this? I don't really know where to start to be honest.