-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
  • 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 Commented Nov 30, 2022 at 10:02

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

it works only for lists! it doesn't work for dictionnary
@tahaelbermaki try dictionary code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.