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

2 Answers 2

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.

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

Comments

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"]

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.