英文:
Transform dataframe Python
问题
UUid | page XY opened | shopcart filled | purchase made |
---|---|---|---|
B32E7AA5B11A40AE | 121 | 92 | 23 |
2AEFB3AEA64140BE | 221 | 21 | 0 |
A0DFAACDEB4D40C1 | 1345 | 1323 | 1321 |
英文:
I have data in one format (summing up events' occurrences by UUID, events in separate rows) but would need it in a different format (events' occurrences in separate columns for each event).
How to, by using Python, transform it from:
UUid | Event | sum |
---|---|---|
B32E7AA5B11A40AE | page XY opened | 121 |
B32E7AA5B11A40AE | shopcart filled | 92 |
B32E7AA5B11A40AE | purchase made | 23 |
2AEFB3AEA64140BE | page XY opened | 221 |
2AEFB3AEA64140BE | shopcart filled | 21 |
A0DFAACDEB4D40C1 | page XY opened | 1345 |
A0DFAACDEB4D40C1 | shopcart filled | 1323 |
A0DFAACDEB4D40C1 | purchase made | 1321 |
To this:
UUid | page XY opened | shopcart filled | purchase made |
---|---|---|---|
B32E7AA5B11A40AE | 121 | 92 | 23 |
2AEFB3AEA64140BE | 221 | 23 | 0 |
A0DFAACDEB4D40C1 | 1345 | 1323 | 1321 |
I don't know where to start.
答案1
得分: 2
尝试使用pivot()
和fillna()
:
df_pivot = df.pivot(index='UUid', columns='Event', values='sum').fillna(0)
df_pivot = df_pivot.reset_index()
df_pivot
输出:
UUid | page XY opened | purchase made | shopcart filled | |
---|---|---|---|---|
0 | 2AEFB3AEA64140BE | 221 | 0 | 21 |
1 | A0DFAACDEB4D40C1 | 1345 | 1321 | 1323 |
2 | B32E7AA5B11A40AE | 121 | 23 | 92 |
英文:
Try using pivot()
and fillna()
df_pivot = df.pivot(index='UUid', columns='Event', values='sum').fillna(0)
df_pivot = df_pivot.reset_index()
df_pivot
Output:
UUid | page XY opened | purchase made | shopcart filled | |
---|---|---|---|---|
0 | 2AEFB3AEA64140BE | 221 | 0 | 21 |
1 | A0DFAACDEB4D40C1 | 1345 | 1321 | 1323 |
2 | B32E7AA5B11A40AE | 121 | 23 | 92 |
答案2
得分: 1
df = pd.pivot_table(df, values='sum', index='UUid', columns='Event', fill_value=0)
df = df.reset_index()
英文:
df = pd.pivot_table(df, values='sum', index='UUid', columns='Event', fill_value=0)
df = df.reset_index()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论