我有dataframe,我需要按“sev”列分组,条件为1&;二,三,四及;5并找到它的计数
有什么办法吗
我已经厌倦了这个,但它给出了sev列中每个单独的值
df.groupby(['sev']).ids.agg('count').to_frame('count').reset_index()
熊猫数据帧-
df = pd.DataFrame({'ids': {0: 'D1791272223', 1: 'V25369085223', 2: 'V25117230523', 3: 'V25104327323', 4: 'V24862169823', 5: 'P3944221523', 6: 'V24776335823', 7: 'V24722584123', 8: 'V24716191923', 9: 'V24575876123', 10: 'V24791923'}, 'status': {0: 'Resolved', 1: 'Resolved', 2: 'Resolved', 3: 'Resolved', 4: 'Open', 5: 'Open', 6: 'Closed', 7: 'Resolved', 8: 'Resolved', 9: 'Open', 10: 'Resolved'}, 'action': {0: 'Comment', 1: 'Implementation', 2: 'Comment', 3: 'Implementation', 4: 'Comment', 5: 'Implementation', 6: 'Comment', 7: 'Comment', 8: 'Implementation', 9: 'Comment', 10: 'Implementation'}, 'sev': {0: 3, 1: 2, 2: 1, 3: 3, 4: 4, 5: 4, 6: 3, 7: 2, 8: 2, 9: 1, 10: 5}})
| ids | status | action | sev |
|--------------|----------|----------------|-----|
| D1791272223 | Resolved | Comment | 3 |
| V25369085223 | Resolved | Implementation | 2 |
| V25117230523 | Resolved | Comment | 1 |
| V25104327323 | Resolved | Implementation | 3 |
| V24862169823 | Open | Comment | 4 |
| P3944221523 | Open | Implementation | 4 |
| V24776335823 | Closed | Comment | 3 |
| V24722584123 | Resolved | Comment | 2 |
| V24716191923 | Resolved | Implementation | 2 |
| V24575876123 | Open | Comment | 1 |
| V24791923 | Resolved | Implementation | 5 |
预期产量
| sev | count | Open count | Closed and Resolved count |
|--------|-------|------------|-----------------------------|
| 1&2 | 5 | 1 | 4 |
| 3 | 3 | 0 | 3 |
| 4&5 | 3 | 2 | 1 |
主要问题是,您需要将严重性级别聚合到较少的类别中,这可以通过
pd.cut
来完成,因为sev
是数字的,并且您希望在连续的时间间隔内执行。如果不是数字或间隔不连续(例如1&;4、2、3&;5),则需要df.replace
和映射字典然后,可以使用
df.pivot_table
或使用groupby/unstack
“手动”进行重塑。我更喜欢groupby
,因为它在其他情况下更灵活输出
相关问题 更多 >
编程相关推荐