【Python 实战基础】Pandas如何统计每月某一数据的平均值
目录
一、实战场景
二、主要知识点
文件读写
基础语法
Pandas
groupby
mean
to_datetime
三、菜鸟实战
1、创建 python 文件
2、运行结果
一、实战场景
实战场景:Pandas如何统计每月某一数据的平均值
二、主要知识点
文件读写
基础语法
Pandas
groupby
mean
to_datetime
三、菜鸟实战
马上安排!
1、创建 python 文件
import pandas as pd#根据timestamp列,得到month月份字段#根据month做聚合,统计每个月的hum的平均值#索引列转换成普通列df = pd.read_csv("london_bike.csv")df['timestamp'] = pd.to_datetime(df['timestamp'])df['hour'] = df['timestamp'].dt.hourdf['month'] = df['timestamp'].dt.month# mean取平均值df_agg = df.groupby("month")["hum"].mean().reset_index()print(df_agg)
2、运行结果
month hum
0 1 77.543376
1 2 74.905813
2 3 71.903781
3 4 67.532337
4 5 66.367272
5 6 68.945148
6 7 64.383525
7 8 67.289420
8 9 71.753228
9 10 77.639621
10 11 78.732517
11 12 81.032345
菜鸟实战,持续学习!