[英]Counting Python pandas Dataframe columns and sorting them by date
I have a DataFrame that looks like;我有一个看起来像的 DataFrame;
ID Tags Priority
Created At
2017-01-01 14:40:00 1234 some_tag P1
2017-01-02 15:00:00 1345 more_tag P4
I want to count all of the different priority tags for each week to look like;我想计算每周所有不同的优先级标签;
Week Priority Count
1 p1 1
p4 1
I've tried the fairly simplet things like:我尝试了相当简单的事情,例如:
print(df.groupby(df.index.date).count())
But that is not giving me what I need.但这并没有给我我需要的东西。
Any suggestions?有什么建议?
DateTimeIndex has a property called weekofyear
which suits your needs. DateTimeIndex 有一个名为
weekofyear
的属性, weekofyear
您的需求。 Furthermore, you do not only want to group on week, but also on priority.此外,您不仅要按周分组,还要按优先级分组。 Luckily, the
groupby
function supports taking several attributes to group by:幸运的是,
groupby
函数支持使用多个属性进行分组:
import datetime
import pandas as pd
a = datetime.datetime.now()
b = datetime.datetime.now()
df = pd.DataFrame({'Created At': [a, b], 'ID': [1234, 1235], 'Priority': ['p1', 'p2']}).set_index('Created At')
print(df)
ID Priority
Created At
2017-02-08 22:25:51.862604 1234 p1
2017-02-08 22:25:57.095862 1235 p2
print(df.groupby([df.index.weekofyear, 'Priority']).count())
ID
Priority
6 p1 1
p2 1
Use pd.TimeGrouper
with freq W-Sat
so that 'Sunday'
and 'Monday'
get grouped together.将
pd.TimeGrouper
与 freq W-Sat
一起使用,以便将'Sunday'
和'Monday'
组合在一起。
df.groupby([pd.TimeGrouper('W-Mon'), 'Priority']).ID.count().to_frame()
ID
Created At Priority
2017-01-07 P1 1
P4 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.