I have a dataframe with several columns that contains a list inside. I want to split this list to different columns. I currently found this question here in stackoverflow, but it seem that it is only splitting the list inside 1 column, which I want to apply to multiple columns containing unequal number of objects in the list.
My df looks something like this:
ID | value_0 | value_1 | value_2 | value_3 | value_4
0 1001|[1001,1002]| None | None | None | None
1 1010|[1010,2001]|[2526,1000]| None | None | None
2 1100|[1234,5678]|[9101,1121]|[3141,5161]|[1718,1920]|[2122,2324]
I want to transform it to:
ID | 0 | 1 | 2 | 3 | 4
0 1001|1001|1002| None | None | None
1 1010|1010|2001| 2526 | 1000 | None
2 1100|1234|5678| 9101 | 1121 | 3141 ....etc.
Currently this is my code but it only outputs a dataframe containing "None" value. I'm not sure how to fix it cause it seem that it is only getting the last column and not really splitting the list.
length = len(list(df.columns.values))-1
for i in range(length):
temp = "value_" + str(i)
x = df[temp]
new_df = pd.DataFrame(df[temp].values.tolist())
The result the new_df that I got is:
| 0
0| None
1| None
2| [2122,2324]
However if I just focus of only 1 column (ie. value_0) it splits the list just fine.
new_df = pd.DataFrame(df['value_0'].values.tolist())
Any help is very much appreciated