This could be answered somewhere but I can't seem to work any of the solutions with what I have...
I have a following dictionary data_dict:
data_dict:
{'df_0':
Name Role Location
0 Gina Assistance NY
1 Jake Officer Brooklyn
2 Boyle Detective 99
3 Scully Assistance NY
4 Diaz Officer Brooklyn
'df_1':
Name Role Location
5 Hitchcock Detective 99
6 Amy Assistance NY
7 Terry Officer Brooklyn
'df_2':
Name Role Location
7 Terry Officer Brooklyn
8 Holt Detective 99
9 Judy Assistance NY
10 Adrian Officer Brooklyn
}
In the dictionary above df_0, df_1, df_2 are the keys for data_dict and each of the keys is a dataframe that contains columns Name, Role, Location
The output I am seeking is a master_df that combines values from data_dict keys and outputs the following results:
master_df:
Name Role Location
0 Gina Assistance NY
1 Jake Officer Brooklyn
2 Boyle Detective 99
3 Scully Assistance NY
4 Diaz Officer Brooklyn
5 Hitchcock Detective 99
6 Amy Assistance NY
7 Terry Officer Brooklyn
8 Holt Detective 99
9 Judy Assistance NY
10 Adrian Officer Brooklyn
Any help / guidance is appreciated!!
You can try using concat function, something like this:
import pandas as pd
master = pd.concat (data_dict.keys())
You an concat
and then drop_duplicates()
:
master_df = pd.concat([df_0, df_1, df_2]).drop_duplicates()
EDIT: Reading more closely... you are doing this from a dictionary... so try dejdej's answer. You may have to drop duplicates, but I didn't test.
Thanks all!
For my purpose I found this solution to output what I needed:
output = pd.DataFrame()
for k in data_dict:
output = output.append(data_dict[k], ignore_index=True)
Thank you everyone for your time and suggestions!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.