【发布时间】:2017-11-05 20:18:01
【问题描述】:
我有一个带有 DatetimeIndex 的数据框:
X
timestamp
2013-01-01 00:00:00 0.788500
2013-01-01 00:30:00 0.761525
2013-01-01 01:00:00 0.751850
2013-01-01 01:30:00 0.746445
2013-01-01 02:00:00 0.688677
我正在使用unstack 重塑它,每半小时间隔一次作为列,日期作为行 - 正如this answer 中所建议的那样。
df.index = [df.index.date, df.index.hour + df.index.minute / 60]
df = df['X'].unstack()
df.head()
0.0 0.5 1.0 1.5 2.0 2.5 \
2013-01-01 0.788500 0.761525 0.751850 0.746445 0.688677 0.652226
2013-01-02 0.799029 0.705590 0.661059 0.627001 0.606560 0.592116
2013-01-03 0.645102 0.597785 0.563410 0.516707 0.495896 0.492416
2013-01-04 0.699592 0.649553 0.598019 0.576290 0.561023 0.537802
2013-01-05 0.782781 0.706697 0.645172 0.627405 0.605972 0.583536
一切都好。 但我现在想对多个数据帧执行相同的程序。最初,我使用的是 2:
for df in [df1,df2]:
df.index = [df.index.date, df.index.hour + df.index.minute / 60]
df = df['X'].unstack()
重建索引有效,但重塑无效:
df1.head()
X
2013-01-01 0.0 0.788500
0.5 0.761525
1.0 0.751850
1.5 0.746445
2.0 0.688677
我想也许我需要一些等价的inplace,以便将未堆叠的数据帧传回df1 和df2
有什么建议吗?
【问题讨论】:
标签: python loops pandas dataframe reshape