Groupby将Pandas DataFrame转换为词典列表

enxuqcxy  于 2022-12-02  发布在  其他
关注(0)|答案(1)|浏览(196)

我有以下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

我想创建一个字典列表,保留列名并将非键的内容(这里是“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},
]}
(...)
]

低效版本为:

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)

如何通过Pandas的groupby达到同样的效果?

xdyibdwo

xdyibdwo1#

你能试试这个吗:

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}]}
]
'''

相关问题