python 按日期筛选数据并计算均值

转载:https://blog.csdn.net/weixin_42782150/article/details/90716533

目的:主要将date相同的数据提取出来,并计算均值。即根据某一列进行分组(groupby函数),再进行运算。

代码:

import numpy as np
import pandas as pd

#read excel file
excel_path = r'path to .xls file'
data = pd.read_excel(excel_path)

data['date'] = data.date
data['surface_el'] = data.surface_el #提取需要进行计算的一列

# Convert the date to datetime64
data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')

for i in data['surface_el'].groupby(data['date']).mean():
    print(i)

参考资料:
根据条件筛选特定行:https://www.cnblogs.com/feigebaqi/p/12766961.html

groupby函数:
https://www.cnblogs.com/Yanjy-OnlyOne/p/11217802.html

groupby函数详解:https://blog.csdn.net/weixin_42782150/article/details/90716533

如何使用groupby函数对数据进行分组:https://zhuanlan.zhihu.com/p/109591980

    原文作者:RS&Hydrology
    原文地址: https://blog.csdn.net/qq_32649321/article/details/118359274
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞