我有以下内容。数据框(每小时时间戳索引):
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']
最佳答案
您需要value_counts
使用 index[0]
,因为数据已排序并且第一个值位于顶部:
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
关于python - 计算 pandas 数据框中非数字列的每日出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461921/