繁体   English   中英

计算pandas数据帧中非数字列的每日出现次数

[英]Computing daily occurrence for non-numeric column in pandas dataframe

我有这个人。 dataframe(每小时时间戳索引):

                      relative_humidity                 condition   fid
2017-08-02 10:00:00               0.49  Chance of a Thunderstorm     1
2017-08-02 11:00:00               0.50  Chance of a Thunderstorm     1
2017-08-02 12:00:00               0.54             Partly Cloudy     1
2017-08-02 13:00:00               0.58             Partly Cloudy     2
2017-08-02 14:00:00               0.68             Partly Cloudy     2

如何计算每天最常出现的情况,并将其放在以日期为索引的数据框中。 还需要通过fid分开?

我试过了:

df.groupby(['fid', pd.Grouper(freq='D')])['condition']

您需要index[0] value_counts ,因为数据已排序且第一个值为top:

d = {'level_1':'date'}
df1 = df.groupby(['fid', pd.Grouper(freq='D')])['condition'] \
       .apply(lambda x: x.value_counts().index[0]).reset_index().rename(columns=d)
print (df1)
   fid       date                 condition
0    1 2017-08-02  Chance of a Thunderstorm
1    2 2017-08-02             Partly Cloudy
df.groupby(['fid',pd.Grouper(freq='D'),'condition']).size().groupby(level=[0,1]).head(1)

输出:

fid              condition               
1    2017-08-02  Chance of a Thunderstorm    2
2    2017-08-02  Partly Cloudy               2
dtype: int64

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM