0
ch_idx = [[1], [2, 3], [3, 4, 5], [2], [3, 4], [3]]

I want to convert this to a dataframe where, for each list, index 1 becomes the first column, idx 2 the second column, etc. This means, as the lists are unequal, that some columns will need to be filled with NaN.

Desired outcome:

1  NaN  NaN
2   3   NaN
3   4    5
2  NaN  NaN
3   4   NaN
3  NaN  NaN

NB: this does not answer my question

syntheso
  • 437
  • 3
  • 17

1 Answers1

3

You can try with:

ch_idx = [[1], [2, 3], [3, 4, 5], [2], [3, 4], [3]]
df=pd.DataFrame(ch_idx).set_index(0)
print(df)

     1    2
0          
1  NaN  NaN
2  3.0  NaN
3  4.0  5.0
2  NaN  NaN
3  4.0  NaN
3  NaN  NaN

Note : you can remove the .set_index() if you dont want

df=pd.DataFrame(ch_idx)
anky
  • 74,114
  • 11
  • 41
  • 70