2

I have a Pandas dataframe that looks like this (sample with two rows):

    cadd_scores_vec        freqs_vec     CLASS
0  [0.001, -4.053424]  (0.0, 0.0, 0.0)      0
1  [0.001, -3.654581]  (0.0, 0.0, 0.0)      0

and I need to de-struct all the content, to single scalar columns, like this:

   col1   col2       col3 col4 col5 col6
0  0.001  -4.053424  0.0  0.0  0.0  0
1  0.001  -3.654581  0.0  0.0  0.0  0

I don't care too much about the new column names; the important thing is to arrange rows into plain vectors as shown above.

How can I achieve it?

peleitor
  • 459
  • 7
  • 24

1 Answers1

3

Two ways to do this with pd.concat:

pd.concat([pd.DataFrame(df[col].values.tolist()) for col in df.columns], axis=1, ignore_index=True)

or

pd.concat([df[col].apply(pd.Series) for col in df.columns], axis=1, ignore_index=True)

ignore_index=True just makes sure you don't get duplicated column names.

rafaelc
  • 57,686
  • 15
  • 58
  • 82