【问题标题】:Groupby to convert Pandas DataFrame to list of dictionariesGroupby 将 Pandas DataFrame 转换为字典列表
【发布时间】:2022-12-01 03:44:11
【问题描述】:

我有以下数据框:

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 获得相同的结果?

【问题讨论】:

    标签: python pandas dictionary group-by


    【解决方案1】:

    你能试试这个吗:

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

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2021-11-10
      相关资源
      最近更新 更多