【发布时间】:2014-05-02 03:46:17
【问题描述】:
我想使用 Pandas 和 Python 遍历我的 .csv 文件,并按季节对数据进行分组,计算一年中每个季节的平均值。目前,季度脚本执行 Jan-Mar、Apr-Jun 等。我希望季节与月份相关 - 11:'Winter',12:'Winter',1:'Winter',2:'Spring',3:'春天',4:'春天',5:'夏天',6:'夏天',7:'夏天',\ 8:'秋天',9:'秋天',10:'秋天'
我有以下数据:
Date,HAD
01/01/1951,1
02/01/1951,-0.13161201
03/01/1951,-0.271796132
04/01/1951,-0.258977158
05/01/1951,-0.198823057
06/01/1951,0.167794502
07/01/1951,0.046093808
08/01/1951,-0.122396694
09/01/1951,-0.121824587
10/01/1951,-0.013002463
这是我目前的代码:
# Iterate through a list of files in a folder looking for .csv files
for csvfilename in glob.glob("C:/Users/n-jones/testdir/output/*.csv"):
# Allocate a new file name for each file and create a new .csv file
csvfilenameonly = "RBI-Seasons-Year" + path_leaf(csvfilename)
with open("C:/Users/n-jones/testdir/season/" + csvfilenameonly, "wb") as outfile:
# Open the input csv file and allow the script to read it
with open(csvfilename, "rb") as infile:
# Create a pandas dataframe to summarise the data
df = pd.read_csv(infile, parse_dates=[0], index_col=[0], dayfirst=True)
mean = df.resample('Q-SEP', how='mean')
# Output to new csv file
mean.to_csv(outfile)
我希望这有点道理。
提前谢谢你!
【问题讨论】:
-
如果您的示例代码是自包含的——也就是说,不依赖于硬盘驱动器上的文件——并且如果您给出预期输出的示例(我很困惑如果您只是想要
group_by()或更多)