1

I have a data frame looks as follows

df1 = df = pd.DataFrame({"a":[[{"x":0,"y":4}], None, [{"x":10,"y":5}], None], 
                     "b":[5, 6, 7, 8]}) 

How can create a seperate column of x and y from it ?

itsMe
  • 747
  • 1
  • 7
  • 23

1 Answers1

3

You can do:

df[['x', 'y']] = df['a'].str[0].apply(pd.Series)

print(df)

                     a  b     x    y
0   [{'x': 0, 'y': 4}]  5   0.0  4.0
1                 None  6   NaN  NaN
2  [{'x': 10, 'y': 5}]  7  10.0  5.0
3                 None  8   NaN  NaN
YOLO
  • 20,181
  • 5
  • 20
  • 40