0

I have a data frame that has three columns, and one of them contains a list:

A            B            C

1            Row1       [rowa,rowb,rowc]

2            Row2       [rowd,rowg,rowf]

I want to split only the list in column C, in different columns, but without loosing data in column A and B.

I tried this:

df = df['C'].apply(pd.Series)

However, this creates a new data frame, and it looses values in column A and B

How can I split column C into different columns, while keeping values in column A and B?

Henry Yik
  • 22,275
  • 4
  • 18
  • 40
Julia
  • 5
  • 4
  • `df.assign(**df['C'].apply(pd.Series))` – yatu Oct 30 '20 at 14:25
  • Welcome to StackOverflow! Please take the time to read this post on how to [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) as well as how to provide a [minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and revise your question accordingly – yatu Oct 30 '20 at 14:26
  • I tried this and I am getting this error: assign() keywords must be strings Even though all keywords in col C are strings – Julia Oct 30 '20 at 14:33
  • Try updating pandas or python – yatu Oct 30 '20 at 14:33

2 Answers2

0

Use pd.concat

df = pd.concat([df,df['C'].apply(pd.Series)],axis=1)
Mehdi Golzadeh
  • 2,594
  • 1
  • 16
  • 28
0

Try assigning to that specific column only, as shown below

df['D'] = df['C'].apply(lambda x: x[0])
df['E'] = df['C'].apply(lambda x: x[1])
df['F'] = df['C'].apply(lambda x: x[2])
Ajay A
  • 1,030
  • 1
  • 7
  • 19