Home > database >  Pandas dataframe from non scalar values and nested dictionary
Pandas dataframe from non scalar values and nested dictionary

Time:10-07

Currently, I get have a dictionary with scalar values and a nested dictionary.

d = {'A': 'a', 'B': 'b', 'C':{0:'z', 1:'y'}}

I would like to put this into a pandas dataframe. However,

pd.DataFrame(d, index=[0])

Gives me only the first value of the nested dictionary. That is, enter image description here.

Any idea how I can get enter image description here instead?

Is there an elegant solution available? I'm hoping to avoid writing any loops.

CodePudding user response:

Put dict to list like:

df = pd.DataFrame([d])
print(df)
   A  B                 C
0  a  b  {0: 'z', 1: 'y'}
    
  • Related