-1

I have a data that contains +15 columns all of them with dictionnary as values. all of the dictionnary has the same keys but different values depending on th column and the key of course. i need to explode them into on data that has the keys as index;this a part of the data

i ve tried this code ! but it only work on one column. i have to do it for all 15 columns and merge them.

data = pd.DataFrame([[i, k, v] for i, d in df[['halstead_vol', 'cyclomatic_complexity']].values for k, v in d.items()],
                  columns=['halstead_vol', 'cyclomatic_complexity', 'h1'])
  • 1
    Please provide a minimal reproducible input and the expected output, currently the question is too vague to give a clear answer without guessing what you might want – mozway Nov 30 '22 at 10:02

1 Answers1

0

If you check explode function documentation in pandas, to explode multiple column you can achieve that in this format:

DataFrame.explode(list(col1col2col3...))

for your case:

df.explode(list('halstead_volcyclomatic_complexityh1'), ignore_index=True)

For dictionary values try this:

new_df = df['halstead_vol'].apply(pd.Series)

Also check this thread on SO for more info.

Oghli
  • 2,200
  • 1
  • 15
  • 37