您还可以保持查询的原样,并使用pandas将其转换为您想要的记录列表
import pandas as pd
df = pd.DataFrame({
"WorkList": ["ADM", "ADM", "BIL", "BIL", "BIL"],
"RecordStatus": ["Closed", "Open", "Closed", "Hold", "Open"],
"QTY": [41, 1, 16, 1, 2]
})
list_records = df.pivot(
index="WorkList", columns="RecordStatus", values="QTY"
).fillna(0).astype(int).reset_index().to_dict(orient='records')
list_records
[{'WorkList': 'ADM', 'Closed': 41, 'Hold': 0, 'Open': 1}, {'WorkList': 'BIL', 'Closed': 16, 'Hold': 1, 'Open': 2}]