关于#jupyter#的问题,如何解决?(语言-python)

#时间序列处理平均数
这种表格要将10分钟数据转换成1小时数据要怎么实现,我用jupyter建立的时间索引为什么不识别?

img

我通过df['年月日时'].dt.strftime('%Y/%m/%d %H')只得到了年月日和时,所以就可以分组聚合求解了。

text='''年月日时,气压/平均值,十米处风速,十米处风向度
2021/6/1 11:40,748.7648,1.166,350.1
2021/6/1 11:50,748.6457,1.266,337.8
2021/6/1 12:00,748.5014,0.992,29.15
2021/6/1 12:10,748.3493,1.48,337.7
2021/6/1 12:20,748.202,1.213,346.5
2021/6/1 12:30,748.0879,1.259,340.4
2021/6/1 12:40,747.9263,1.064,319.4
2021/6/1 12:50,747.7966,1.253,341.2
2021/6/1 13:00,747.7117,1.268,328.4
2021/6/1 13:10,747.695,1.054,345.6
2021/6/1 13:20,747.5385,1.156,352
2021/6/1 13:30,747.4217,1.02,340.5
2021/6/1 13:40,747.2539,0.818,281.3
2021/6/1 13:50,747.1719,0.702,236.1
2021/6/1 14:00,747.1819,0.732,293.3
2021/6/1 14:10,747.1974,0.476,240.6
2021/6/1 14:20,747.0383,0.521,237.3
2021/6/1 14:30,746.9448,1.032,335.9
2021/6/1 14:40,746.9276,1.295,349.1'''

import pandas as pd
from io import StringIO

df = pd.read_csv(StringIO(text))
df['年月日时'] = pd.to_datetime(df['年月日时'])
df['年月日时'] = df['年月日时'].dt.strftime('%Y/%m/%d %H')
df = df.set_index('年月日时')
result_df = df.groupby(df.index).agg('sum')
print(result_df)

img


代码修改为excel的如下,下面有注释可以看下:

import pandas as pd

df = pd.read_excel('data.xlsx', engine='openpyxl', skiprows=[0, 2])  # 看了下你原始数据里面,第1行和第3行是不需要的,所以科研通过skiprows这个参数过滤掉

df['年月日时'] = pd.to_datetime(df['年月日时'])
df['年月日时'] = df['年月日时'].dt.strftime('%Y/%m/%d %H')
df = df.set_index('年月日时')
result_df = df.groupby(df.index).agg('sum')