Home > Software engineering >  Split tuples columns in pandas dataframe
Split tuples columns in pandas dataframe

Time:10-10

How can I split this dataframe in two separate columns?

enter image description here

I have tried:

df_partes1=pd.DataFrame(df_partes1[1].values.tolist(), index=df_partes1[0], columns=['x','y'])

but:

Shape of passed values is (12, 1), indices imply (12, 2)

CodePudding user response:

You can use:

df = pd.DataFrame(data={0: ['Neck', 'RShoulder', 'LShoulder', 'RElbow', 'RWrist', 'LElbow'],
                        1: [None, None, (840, 183), None, None, (936,255)]})

df[['new_col_1', 'new_col_2']] = df[1].apply(pd.Series)

Output:

           0           1  new_col_1  new_col_2
0       Neck        None        NaN        NaN
1  RShoulder        None        NaN        NaN
2  LShoulder  (840, 183)      840.0      183.0
3     RElbow        None        NaN        NaN
4     RWrist        None        NaN        NaN
5     LElbow  (936, 255)      936.0      255.0
  • Related