2

I have this dataframe here that looks like this ( I can't upload images yet so I used plain text instead):

      #  name  things
0    a   [d,e,f]
1    b   [g,f,s,w]
2    c   [s]

and I want to convert it to this:

#  name  things1  things2  things3 things4
0    a     d       e         f
1    b     g       f         s       w
2    c     s

Is there any way that I can do this clean and fast? I'm just a beginner so I can't understand too complex code. Thanks very much.

luigigi
  • 4,146
  • 1
  • 13
  • 30

1 Answers1

1

You are looking for the following line:

df2 = pd.concat([df.name, df.things.apply(pd.Series)], axis=1)

With a reproducible example below (same transformation, but with full code so that you can repoduce it and understand it):

# Create dataset
import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([["a", "b"],[["d", "e", "f"],["d", "e", "f"]]])).T
df.columns = ["value", "list"]

# Apply transformation
df2 = pd.concat([df.value, df.liste.apply(pd.Series)], axis=1)
thibaultbl
  • 886
  • 1
  • 10
  • 20