熊猫在行上查找交配条目并将这些行中的列合并为一列
我有一个这样的数据字段。
索引 | 产品 | 购买_地址 | 订单_日期 |
---|---|---|---|
0 | A | 604 Cherry st, Dallas | 2019-10-28 |
1 | B | 225 5th st, LA | 2019-10-29 |
2 | C | 604 Cherry st, Dallas | 2019-10-28 |
3 | D | 225 5th st, LA | 2019-10 -29 |
4 | E | 967 第 12 街,纽约 | 2019-10-27 |
5 | F | 967 12th st,纽约 | 2019-10-27 |
6 | A | 628 Jefferson St,纽约 | 2019-10-20 |
7 | B | 628 Jefferson St,纽约 | 2019-10-20 |
8 | A | 694 Meadow St,亚特兰大 | 2019 -10-25 |
9 | B | 694 Meadow St, 亚特兰大 | 2019-10-25 |
10 | C | 27 Wilson St, Austin | 2019-10-26 |
11 | D | 27 Wilson St, Austin | 2019-10-26 |
我需要创建一个新的数据字段,在其中将产品合并为一个如果地址和订单日期相同(意味着它们是同时订购的),则列。
df 应如下所示:
Index | Product | Purchase_Address |
---|---|---|
0 | A, C | 604 Cherry st, Dallas |
1 | B, D | 225 5th st, LA |
2 | E, F | 967 12th st, NY |
3 | A, B | 628 Jefferson St, NY |
4 | A, B | 694 Meadow St, 亚特兰大 |
5 | C, D | 27 Wilson St, 奥斯汀 |
然后从 df 开始,我在那里数了组合发生的次数:
Index | Product_Combination | Nr_Of_Times |
---|---|---|
0 | A, C | 1 |
1 | B, D | 1 |
2 | E, F | 1 |
4 | A, B | 2 |
5 | C, D | 1 |
我如何实现这样的目标? 谢谢!
I have a data field like this.
Index | Product | Purchase_Address | Order_Date |
---|---|---|---|
0 | A | 604 Cherry st, Dallas | 2019-10-28 |
1 | B | 225 5th st, LA | 2019-10-29 |
2 | C | 604 Cherry st, Dallas | 2019-10-28 |
3 | D | 225 5th st, LA | 2019-10-29 |
4 | E | 967 12th st, NY | 2019-10-27 |
5 | F | 967 12th st, NY | 2019-10-27 |
6 | A | 628 Jefferson St, NY | 2019-10-20 |
7 | B | 628 Jefferson St, NY | 2019-10-20 |
8 | A | 694 Meadow St, Atlanta | 2019-10-25 |
9 | B | 694 Meadow St, Atlanta | 2019-10-25 |
10 | C | 27 Wilson St, Austin | 2019-10-26 |
11 | D | 27 Wilson St, Austin | 2019-10-26 |
I need to make a new data field where I would merge the products into a single column if the address and order date are the same (meaning they where ordered at the same time).
The df should look something like this:
Index | Product | Purchase_Address |
---|---|---|
0 | A, C | 604 Cherry st, Dallas |
1 | B, D | 225 5th st, LA |
2 | E, F | 967 12th st, NY |
3 | A, B | 628 Jefferson St, NY |
4 | A, B | 694 Meadow St, Atlanta |
5 | C, D | 27 Wilson St, Austin |
And then from that a df, where I count the number of times a combination has happened:
Index | Product_Combination | Nr_Of_Times |
---|---|---|
0 | A, C | 1 |
1 | B, D | 1 |
2 | E, F | 1 |
4 | A, B | 2 |
5 | C, D | 1 |
How would I achieve something like this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
Groupby.agg
与Groupby.count
和Series.to_frame
结合使用:Use
Groupby.agg
withGroupby.count
andSeries.to_frame
: