热线电话:13121318867

登录
2019-02-25 阅读量: 772
Pandas GroupBy查询 问问题

我在pandas中有一个数据帧,如下所示: 我的pandas数据帧的快照

现在我想要像下面那样转换数据框,其中属性“类别”根据排序的日期值(%m /%d /%Y)被每个customerid的分隔符连接起来。具有较早日期的订单的类别首先列在相应的客户ID中。

期望/转换的数据框架

解决办法:首先通过转换列to_datetime,然后sort_values和最后groupby用join:

df['Date'] = pd.to_datetime(df['Date'], format='%m/%d/%Y')

df = (df.sort_values(['customerid','Age','Date'])

.groupby(['customerid','Age'])['category']

.agg(', '.join)

.reset_index())

print (df)

customerid Age category

0 1 10 Electronics, Clothing

1 2 25 Grocery, Clothing

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子