【发布时间】:2022-01-25 21:38:41
【问题描述】:
我应该做 groupby(country and product) 和 Value column 应该包含 count(id) where status is closed,我需要返回所有剩余的列。
Sample input
id status ticket_time product country name
126 open 2021-10-04 01:20:00 Broad A metric
299 open 2021-10-02 00:00:00 Fixed B metric
376 closed 2021-10-01 00:00:00 Fixed C metric
370 closed 2021-10-04 00:00:00 Broad C metric
372 closed 2021-10-04 00:00:00 TV D metric
605 closed 2021-10-01 00:00:00 TV D metric
输出格式示例
country product name ticket_time Value(count(id)where status closed)
D TV metric YYYY-MM-DD HH:MM:SS 2
C Broad metric YYYY-MM-DD HH:MM:SS 1
C Fixed metric YYYY-MM-DD HH:MM:SS 1
.... ... .... ... ...
我尝试了以下代码:
df1 = df[df['status'] == 'closed']
df1['Value'] = df1.groupby(['country', 'product'])['status'].transform('size')
df = df1.drop_duplicates(['country', 'product']).drop('status',axis=1).drop(['id'], axis = 1)
有没有更好的方法来解决这个问题?
【问题讨论】:
-
请提供您的数据框作为代码
标签: python python-3.x pandas dataframe pandas-groupby