Home > Enterprise >  how to convert dictionary where values are stored in a list to df
how to convert dictionary where values are stored in a list to df

Time:11-02

I have a dictionary that I want to convert into DataFrame, the problem I'm having is my dictionary is in the following format:


sample_dict = {'test':['test string',['feature1','feature2', 'feature3']]}

When trying to convert using something like

df = pd.DataFrame.from_dict([sample_dict])

I get:

enter image description here

What I'm trying to achieve is something like:

enter image description here

Any ideas?

CodePudding user response:

Add orient

out = pd.DataFrame.from_dict(sample_dict, orient='index')
Out[287]: 
                0                               1
test  test string  [feature1, feature2, feature3]

CodePudding user response:

Can use from_records as well.

df = pd.DataFrame.from_records(d).T

                0                               1
test  test string  [feature1, feature2, feature3]
  • Related