【发布时间】:2021-09-03 04:26:27
【问题描述】:
我是 python 新手,所以请帮助我。
我有一个数据框
df
id status emailaddress ownername Key
a pending a.gmail.com as 1
a pending a.gmail.com as 2
a submitted a.gmail.com as 3
a submitted a.gmail.com as 4
b pending b.gmail.com bs 1
b pending b.gmail.com bs 2
b pending b.gmail.com bs 3
b pending b.gmail.com bs 4
b pending b.gmail.com bs 1
c submitted c.gmail.com bs 1
c submitted c.gmail.com bs 3
c submitted c.gmail.com bs 4
我想根据每个 id 的状态计算 Key 列的计数。
此外,如果状态任何一个状态 = 未决,则该用户的整体状态将在摘要表中处于未决状态。
Total Key = count (Key) where status(pending)
CompletedKey = count (Key) where status(submitted)
预期的 df_summary
id status emailaddress ownername TotalKey CompletedKey RemainingKey
a pending a.gmail.com as 4 2 2
b pending b.gmail.com bs 5 0 4
c submitted c.gmail.com cs 3 3 0
我的代码
dfResponse = df.groupby(['id','ownername','status','emailaddress'])['Key'].count().reset_index(name="TotalKey")
【问题讨论】: