1

I am struggling with converting a dictionary into a dataframe.

There are already a lot of answers showing how to do it in the "wide format" like https://stackoverflow.com/a/52819186/6912069 but I would like to do something different, preferably not using loops.

Consider the following example:

I have a dictionary like this one

d_test = {'A': [1, 2], 'B': [3]}

and I'd like to get a dataframe like

index id    values
0     A     1
1     A     2
2     B     3

The index can be a normal consecutive integer column. By recycling I mean turning 'A'=[1, 2] into two rows having A in the id column and the values in the values column. This way I would have a "long format" dataframe of the dictionary items.

It seems to be a very basic thing to do, but I was wondering if there is an elegant pythonic way to achieve this. Many thanks for your help.

1 Answer 1

1

I would create 2 lists. One from the keys, and other one from the values of the dictionary. As you defined the lists you can pass the lists into the DataFrame.

import pandas as pd
dic = {'A': [1, 2], 'B': [3], 'D': [4, 5, 6]}

keys = []
values = []

for key, value in dic.items():
    for v in value:
        keys.append(key)
        values.append(v)

df = pd.DataFrame(
    {'id': keys,
     'values': values,
    })
print(df)
Sign up to request clarification or add additional context in comments.

Comments

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.