【发布时间】:2021-11-27 12:24:36
【问题描述】:
我有一个数据框,我将其转换为 defaultdict,其中 'id' 列作为键,其余列作为值,所以我执行以下操作:
d = {'id': [1,1,1,1,2,2,3,3,3,4,4,4,4],
'label':['A','A','B','G','A','BB','C','C','A','BB','B','AA','AA']
,'amount':[2,-12,12,-12,5,-5,2,3,5,3,3,10,10]}
df = pd.DataFrame(d)
from collections import defaultdict
import pandas as pd
dd = defaultdict(list)
# turn df into a dictionary groyped by the 'id'
for index,row in df.iterrows():
dd[row["id"]].append(
{
"description": row["label"],
'amount':row['amount'] })
dd
defaultdict(list,
{1:[{'id':1, 'description': 'A', 'amount': 2},
{'id': 1, 'description': 'A', 'amount':-12},
{'id': 1, 'description': 'B', 'amount': 12},
{'id': 1, 'description': 'G', 'amount':-12}],
2:[{'id': 2, 'description': 'A', 'amount': 5},
{ 'id': 2, 'description': 'BB', 'amount':-5}],
3:[{'id': 3, 'description': 'C', 'amount': 2},
{'id': 3, 'description': 'C', 'amount': 3},
{'id': 3, 'description': 'A', 'amount': 5}],
4:[{'id': 4, 'description': 'BB', 'amount': 3},
{'id': 4, 'description': 'B', 'amount': 3},
{'id': 4, 'description': 'AA', 'amount': 10},
{'id': 4, 'description': 'AA', 'amount':10}]})
我想做的是像熊猫数据框一样操作字典。例如,我想检查每个用户的“描述”和“数量”是否在记录中相等。 对于具体的例子,我希望我想要的字典看起来像这样:
defaultdict(list,{4: [{'id': 4, 'description': 'AA', 'amount': 10},
{'id': 4, 'description': 'AA', 'amount': 10}]})
【问题讨论】:
-
嗯,所以
defaultdict需要 pandas 方法吗?什么是理由?为什么不将 pandas 用于 pandas 方法并在最后一步将输出转换为defaultdict?
标签: python pandas list dataframe dictionary