2

I have the following dictionary (df) of dataframes:

([('Austria',
       Name                                     Value
 0     3 BG EMCore Convertibles Global CHF R T    5
 1     3 BG EMCore Convertibles Global R T        6
 [2 rows x 2 columns]),

('Belgium',
        Name                                    Value
 0      AG Life Alternative Investments           7
 1      AG Life Balanced                          1
 2      AG Life Bonds Global                      2
 3      AG Life Bonds Indexed                     7
 [4 rows x 2 columns])])

I want to convert the data into a single dataframe and simultaneously set as additional column the key strings

I first convert the dictionary into a list of dataframes:

mylist=[i[1] for i in df.items()]

Then I can merge the dataframes into a single dataframe by:

master_frame=pd.concat(mylist, sort=True)

But I want to set as additional column the keys from the dictionary

Expected result if like this:

       Name                                     Value     Country
 0     3 BG EMCore Convertibles Global CHF R T    5       Austria
 1     3 BG EMCore Convertibles Global R T        6       Austria
 2      AG Life Alternative Investments           7       Belgium
 3      AG Life Balanced                          1       Belgium
 4      AG Life Bonds Global                      2       Belgium
 5      AG Life Bonds Indexed                     7       Belgium

Thanks in advance

2 Answers 2

5

Have you tried this:

final_df = pd.DataFrame()
for key, value in dictionary.items():
     df = value
     df.loc[:,'Country'] = key
     final_df = pd.concat([df, final_df], 0)
final_df
2
  • Works. I needed to add axis=0 in the concat part though and delete the 1. Aug 13, 2019 at 6:03
  • @MartinYordanovGeorgiev Glad it helped.
    – Jorge
    Aug 13, 2019 at 13:38
4

For a dict like:

dfs = {'Austria': AustriaDF, 'Belgium': BelgiumDF}

You can just add the new column based on the keys and then concat those:

for country, df in dfs.items():
    df['Country'] = country
master_frame = pd.concat(sorted(dfs.values(), key=lambda df: df['Country'][0]), ignore_index=True)

Value for master_frame:

                                      Name  Value  Country
0  3 BG EMCore Convertibles Global CHF R T      5  Austria
1      3 BG EMCore Convertibles Global R T      6  Austria
2          AG Life Alternative Investments      7  Belgium
3                         AG Life Balanced      1  Belgium
4                     AG Life Bonds Global      2  Belgium
5                    AG Life Bonds Indexed      7  Belgium
2
  • Works. Needed to replace dfs.iteritems with dfs.items though. Aug 13, 2019 at 6:04
  • Yeah, sorry, will fix, old Python 2 habit.
    – mVChr
    Aug 13, 2019 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.