Home > database >  In Pandas, combine dictionary with a single value for output dataframe
In Pandas, combine dictionary with a single value for output dataframe

Time:02-24

If I have a number of dictionaries, how could I best combine them into a single pandas DataFrame? I would also like to have an added column, with a given value for each "batch" of dictionaries.

Example inputs:


# Declare initial parameters:
bonuscolumn_name = 'Col_A'
desired_pd = pd.DataFrame(columns=['bonuscolumn_name', 'name', 'age'])

# First batch of dictionaries:
bonuscolumn_value1 = 'somevalue'
dict1 = {'name':'Sam', 'age':26}
dict2 = {'name':'Albert', 'age':21}
dict3 = {'name':'Brock', 'age':57}

# Second batch of dictionaries:
bonuscolumn_value1 = 'diffvalue'
dict4 = {'name':'Fred', 'age':14}
dict5 = {'name':'Philbert', 'age':20}

Desired Output:

Col_A name age
somevalue Sam 26
somevalue Tyler 21
somevalue Brock 57
diffvalue Fred 14
diffvalue Philbert 20

CodePudding user response:

You can create a dataframe for each diciontary an then use:

df = pd.concat([dict1, dict2, dict3, dict4, dict5, dict6], keys=[dict1, dict2, dict3, dict4, dict5, dict6])

Then:

df.reset_index(inplace=True)

CodePudding user response:

Try:

# First batch of dictionaries:
bonuscolumn_value1 = 'somevalue'
dict1 = {'name':'Sam', 'age':26}
dict2 = {'name':'Albert', 'age':21}
dict3 = {'name':'Brock', 'age':57}
df1 = pd.DataFrame([dict1, dict2, dict3]).assign(bonuscolumn_name=bonuscolumn_value1)

# Second batch of dictionaries:
bonuscolumn_value1 = 'diffvalue'
dict4 = {'name':'Fred', 'age':14}
dict5 = {'name':'Philbert', 'age':20}
df2 = pd.DataFrame([dict4, dict5]).assign(bonuscolumn_name=bonuscolumn_value1)

df = pd.concat([df1, df2], ignore_index=True)

Final output:

>>> df
       name  age bonuscolumn_name
0       Sam   26        somevalue
1    Albert   21        somevalue
2     Brock   57        somevalue
3      Fred   14        diffvalue
4  Philbert   20        diffvalue
  • Related