1 回答
TA贡献1995条经验 获得超2个赞
您可以sort在 DATE 列上。但是你需要记住sort=False在你的 groupby 中做,否则它将使用字符串排序在那里排序。此外,您每年的重复代码可以替换为单个 groupby,添加year到分组键中。然后,您将分别写入不同的文件,index=False这就是您摆脱索引的方式。
import numpy as np
import pandas as pd
df = pd.read_csv('filteredData.csv')
df['DATE'] = pd.to_datetime(df['DATE'])
df['year'] = df['DATE'].dt.year # Datetime has this attribute already
df = df.sort_values(['NAME', 'DATE']) # Output will be in order within each Name
df = (df[df.year.between(2016,2017)] # Only 2016 and 2017
.groupby(['year', 'NAME', 'Month'], sort=False)['SNOW']
.mean().reset_index())
for year,gp in df.groupby('year'): # Write files separately by year
gp[['NAME', 'Month', 'SNOW']].to_csv(f'average{year}.csv', index=False)
添加回答
举报