python - 计算 pandas 数据框中非数字列的每日出现次数

标签 python pandas

我有以下内容。数据框(每小时时间戳索引):

                      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/

相关文章:

python - 迭代 Pandas 中的列并更改单元格的值 - Python

python-3.x - Pandas 合并意外产生后缀

python - 使用 Python 的 SSL3 POST

python - 是否可以与 python pandas 进行模糊匹配合并?

python - Pytables 检查列是否存在

python - 循环遍历 groupby 为 pandas 中的名称分配数字

python - 单击时 OptionMenu 不会显示第一个选项 (Tkinter)

可自行下载的 Python Web 应用程序

python - 在 TensorFlow 中创建距离矩阵

python - Bottle 网络应用程序不提供静态 css 文件