我做了一个 sql 查询,然后data = pd.read_sql(query, connection)我有了下表,
ID ITEM TYPE_USER Count
711757 item1 type1 1
711757 item2 type1 1
711757 item3 type1 1
711794 item1 type2 1
711794 item2 type2 1
711541 item2 type3 1
. . . .
. . . .
但我需要创建以下数据框
ID item1 item2 item3 TYPE_USER
711757 1 1 1 type1
711794 1 1 0 type2
711541 0 1 0 type3
所以,我的想法是采取
`data.pivot(index='ID', columns = 'ITEM', values='Count')
但是,这给了我以下数据框
ID item1 item2 item3
0 711757 1 1 1
1 711794 1 1 0
2 711541 0 1 0
在这一点上,我不知道如何加入“TYPE_USER”列,任何想法将不胜感激!谢谢!