【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.hour
df['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

菜鸟实战,持续学习!

Original: https://blog.csdn.net/qq_39816613/article/details/126251276
Author: 菜鸟实战
Title: 【Python 实战基础】Pandas如何统计每月某一数据的平均值

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/751849/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球