我有以下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达到同样的效果?
1条答案
按热度按时间xdyibdwo1#
你能试试这个吗: