简体   繁体   English

Groupby 将 Pandas DataFrame 转换为字典列表

[英]Groupby to convert Pandas DataFrame to list of dictionaries

I have the following DataFrame:我有以下 DataFrame:

print(df)

business_id     software_id     quantity     price     inventory_level
   1234              abc           10        25.5            5
   4820              bce           40        21.9            2
   1492              abc           59        25.3            1
   1234              abc           55        11.3            0

I would like to create a list of dictionaries, keeping column names and storing what's not a key - here "business_id" and "software_id" - as a list of dictionaries, using Pandas' groupby and thus obtaining:我想创建一个字典列表,保留列名并存储不是键的内容 - 这里是“business_id”和“software_id” - 作为字典列表,使用 Pandas 的 groupby 并因此获得:

[

{
business_id: 1234,
software_id: abc,
transactions: [
      {quantity: 10, price: 25.5, inventory_level:5},
      {quantity: 55, price: 11.3, inventory_level:0},
]}
(...)
]

The inefficient version would be:低效的版本是:

keys_l = ["business_id", "software_id"]
keys_df = df.filter(keys_l).drop_duplicates()

chunk_l = []
for _, row in keys_df.iterrows():
    
    # --- Subset original DataFrame ---
    chunk_df = df[(df[keys_l]==row).all(axis=1)]
    
    # --- Create baseline keys with keys ---
    chunk_dict = {key: value for key, value in zip(row.index, row.values)}
    
    # --- Add bucketed data points ---
    chunk_dict["transactions"] = chunk_df.drop(keys_l, axis=1).to_dict(orient="records")
   
    # --- Append to list to create a list of dictionaries ---
    chunk_l.append(chunk_dict)

How can I achieve the same result through Pandas'groupby?如何通过 Pandas'groupby 获得相同的结果?

Can you try this:你能试试这个吗:

dfx=df.groupby(['business_id','software_id']).agg(list).T
final=[]
for i in dfx.columns:
    final.append({'business_id':i[0], 'software_id':i[1],
                 'transactions':[{'quantity':dfx[i]['quantity'][j],'price':dfx[i]['price'][j],'inventory_level':dfx[i]['inventory_level'][j]} for j in range(len(dfx[i][0]))]})

print(final)
'''
[
{'business_id': 1234, 'software_id': 'abc', 'transactions': [{'quantity': 10, 'price': 25.5, 'inventory_level': 5}, {'quantity': 55, 'price': 11.3, 'inventory_level': 0}]},

{'business_id': 1492, 'software_id': 'abc', 'transactions': [{'quantity': 59, 'price': 25.3, 'inventory_level': 1}]},

{'business_id': 4820, 'software_id': 'bce', 'transactions': [{'quantity': 40, 'price': 21.9, 'inventory_level': 2}]}
]
'''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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