我使用的是Python 3.6,这是我的代码:
trainFile = r'C:\download\combine\Combine_1.csv'
pwd = os.getcwd()
os.chdir(os.path.dirname(trainFile))
df = pd.read_csv(os.path.basename(trainFile))
os.chdir(pwd)
df['date'] = pd.to_datetime(df['date'])
df = df.set_index('date')
week = df.resample('w').mean()
week.to_csv('week_combine.csv')
我尝试将每日数据制作成每周数据,但现在每周从每个星期日开始,我怎样才能将其更改为每周一?
例如,现在结果如下:
...
2017-09-10
2017-09-17
2017-09-24
...
但我想将其更改为:
...
2017-09-11
2017-09-18
2017-09-25
...
有什么想法吗?
答案 0 :(得分:4)
需要W-MON
anchored offset:
rng = pd.date_range('2017-09-09', periods=15)
df = pd.DataFrame({'a': range(15)}, index=rng)
week = df.resample('W-MON').mean()
print (week)
a
2017-09-11 1
2017-09-18 6
2017-09-25 12