2

I've seen this issue construct-pandas-dataframe-from-items-in-nested-dictionary. The difference is that here I consider Nested Dataframe In Dict.

My problem may seem to be confusing. Here's a simple example. Nested dataframe in dict:

d1=pd.DataFrame({'s':[1]})
d2=pd.DataFrame({'s':[2]})
d3=pd.DataFrame({'s':[3]})
d={'a':d1,
  'b':d2,
  'c':d3}

What I want is a dataframe like:

a b c
1 2 3
Shaido
  • 25,575
  • 21
  • 68
  • 72
balalala
  • 92
  • 9

2 Answers2

1

Since you dictionary contains dataframes as values, you can simply use pd.concat on the dictionary itself:

df = pd.concat(d, axis=1)
df.columns = df.columns.droplevel(-1)

The resulting dataframe will have multiindex columns, the last line will fix that.

Shaido
  • 25,575
  • 21
  • 68
  • 72
0

I think you need to concat dataframes

dff = pd.DataFrame()
for i in list(d.keys()):
    dff = pd.concat([dff,d[i]],axis=1)

dff.columns = ["a","b","c"]
Rajith Thennakoon
  • 3,604
  • 2
  • 12
  • 21