[英]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.