I have a really simple Pandas dataframe where each cell contains a list. I'd like to split each element of the list into it's own column. I can do that by exporting the values and then creating a new dataframe. This doesn't seem like a good way to do this especially, if my dataframe had a column aside from the list column.
import pandas as pd df = pd.DataFrame(data=[[[8,10,12]], [[7,9,11]]]) df = pd.DataFrame(data=[x[0] for x in df.values]) Desired output:
0 1 2 0 8 10 12 1 7 9 11 Follow-up based on @Psidom answer:
If I did have a second column:
df = pd.DataFrame(data=[[[8,10,12], 'A'], [[7,9,11], 'B']]) How do I not loose the other column?
Desired output:
0 1 2 3 0 8 10 12 A 1 7 9 11 B
You can loop through the Series with apply() function and convert each list to a Series, this automatically expand the list as a series in the column direction:
df[0].apply(pd.Series) # 0 1 2 #0 8 10 12 #1 7 9 11 Update: To keep other columns of the data frame, you can concatenate the result with the columns you want to keep:
pd.concat([df[0].apply(pd.Series), df[1]], axis = 1) # 0 1 2 1 #0 8 10 12 A #1 7 9 11 B
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With