I have a dictionary of about 600 dataframes for connectome metrics for those 600 participants. I’m making a dummy dictionary though for conciseness. The dataframes all have 1 row. Ignore the fact that the actual meaning of “mean” as the column names are not what is of the row samples I give below, they’re just the column names of all the dataframes I want to merge. Each of the keys are unique.
Two example dataframes would look like this:
Key = "Sonic"
Value_df =
mean median stdev min max count
1 3 7 9 22 11
Key = "Tails"
Value_df =
mean median stdev min max count
3 5 11 22 33 14
where…
{"Sonic":Dataframe, "Tails":Dataframe}
How do I merge the dictionary’s dataframe and make the key values its own column in the final dataframe?
Subject mean median stdev min max count
Sonic 1 3 7 9 22 11
Tails 3 5 11 22 33 14
I’ve tried from_dict
and I didn’t have any luck.
You need to concat
, then reset_index
:
dic = {'Sonic': sonic_df, 'Tails': tails_df}
out = (pd.concat(dic, names=['Subject']).reset_index(0)
.reset_index(drop=True) # optional
)
Output:
Subject mean median stdev min max count
0 Sonic 1 3 7 9 22 11
1 Tails 3 5 11 22 33 14