简体   繁体   中英

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

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

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)

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM