我有一组这样的数据:
gasPoint
我想为每行的每个值重新排列日期+时间。类似的东西:
Unnamed: 0 0:30 1:00 1:30 2:00 2:30 3:00 3:30 4:00 4:30 ... \
0 2016-01-01 26.9 26.4 26.9 26.1 26.4 26.7 26.5 25.5 25.4 ...
1 2016-01-02 26.8 25.9 25.7 26.0 25.2 25.3 25.6 25.0 25.1 ...
2 2016-01-03 25.6 25.4 25.2 25.5 24.5 24.5 24.9 24.6 24.3 ...
3 2016-01-04 24.9 24.2 23.7 23.5 23.4 23.4 23.6 23.3 23.7 ...
.. ... ... ... ... ... ... ... ... ... ... ...
361 2016-12-27 15.6 15.5 15.5 15.7 16.1 16.2 16.3 16.2 16.2 ...
362 2016-12-28 15.3 15.7 16.3 16.4 16.2 16.1 16.3 16.1 16.2 ...
363 2016-12-29 15.9 16.3 16.1 16.4 16.4 16.2 16.1 16.1 15.9 ...
364 2016-12-30 16.2 16.3 16.3 16.3 16.2 16.3 16.3 16.3 16.3 ...
365 2016-12-31 16.1 16.0 16.2 16.3 16.2 16.2 16.3 16.3 16.3 ...
但是我不知道如何在没有迭代的情况下将值附加到新的数据帧中(由于我有几个大数据集,因此非常慢)
我试过像
这样的东西0 2016-01-01 0:30 26.9
1 2016-01-01 1:00 26.4
2 2016-01-01 1:30 26.9
3 2016-01-01 2:00 26.1
但这不对 - 这是我想不到的数据结构
想法?感谢
答案 0 :(得分:2)
您可以将pandas stack()用于此
df.set_index('Unnamed: 0').stack().reset_index()
Unnamed: 0 level_1 0
0 2016-01-01 0:30 26.9
1 2016-01-01 1:00 26.4
2 2016-01-01 1:30 26.9
3 2016-01-01 2:00 26.1
4 2016-01-01 2:30 26.4
5 2016-01-01 3:00 26.7
6 2016-01-01 3:30 26.5
7 2016-01-01 4:00 25.5
8 2016-01-01 4:30 25.4
9 2016-01-02 0:30 26.8
10 2016-01-02 1:00 25.9
11 2016-01-02 1:30 25.7
12 2016-01-02 2:00 26.0
13 2016-01-02 2:30 25.2
14 2016-01-02 3:00 25.3
15 2016-01-02 3:30 25.6
答案 1 :(得分:1)
@ A-Za-z的答案是我的第一个想法......但是如果你想让新的索引值成为包含列中时间元素的时间戳......
set_index
将日期记录到索引中,以便我可以轻松获取所有值numpy
广播生成所有时间戳ravel
d值和时间戳d1 = df.set_index('Unnamed: 0')
td = pd.to_timedelta(d1.columns + ':00').values
ts = pd.to_datetime(d1.index).values
pd.Series(
d1.values.ravel(),
(ts[:, None] + td).ravel()
)
输出:
2016-01-01 00:30:00 26.9
2016-01-01 01:00:00 26.4
2016-01-01 01:30:00 26.9
2016-01-01 02:00:00 26.1
2016-01-01 02:30:00 26.4
2016-01-01 03:00:00 26.7
2016-01-01 03:30:00 26.5
2016-01-01 04:00:00 25.5
2016-01-01 04:30:00 25.4
2016-01-02 00:30:00 26.8
2016-01-02 01:00:00 25.9
2016-01-02 01:30:00 25.7
2016-01-02 02:00:00 26.0
2016-01-02 02:30:00 25.2
2016-01-02 03:00:00 25.3
2016-01-02 03:30:00 25.6
2016-01-02 04:00:00 25.0
2016-01-02 04:30:00 25.1
2016-01-03 00:30:00 25.6
2016-01-03 01:00:00 25.4
2016-01-03 01:30:00 25.2
2016-01-03 02:00:00 25.5
2016-01-03 02:30:00 24.5
2016-01-03 03:00:00 24.5
2016-01-03 03:30:00 24.9
2016-01-03 04:00:00 24.6
2016-01-03 04:30:00 24.3
2016-01-04 00:30:00 24.9
2016-01-04 01:00:00 24.2
2016-01-04 01:30:00 23.7
...
2016-12-28 03:30:00 16.3
2016-12-28 04:00:00 16.1
2016-12-28 04:30:00 16.2
2016-12-29 00:30:00 15.9
2016-12-29 01:00:00 16.3
2016-12-29 01:30:00 16.1
2016-12-29 02:00:00 16.4
2016-12-29 02:30:00 16.4
2016-12-29 03:00:00 16.2
2016-12-29 03:30:00 16.1
2016-12-29 04:00:00 16.1
2016-12-29 04:30:00 15.9
2016-12-30 00:30:00 16.2
2016-12-30 01:00:00 16.3
2016-12-30 01:30:00 16.3
2016-12-30 02:00:00 16.3
2016-12-30 02:30:00 16.2
2016-12-30 03:00:00 16.3
2016-12-30 03:30:00 16.3
2016-12-30 04:00:00 16.3
2016-12-30 04:30:00 16.3
2016-12-31 00:30:00 16.1
2016-12-31 01:00:00 16.0
2016-12-31 01:30:00 16.2
2016-12-31 02:00:00 16.3
2016-12-31 02:30:00 16.2
2016-12-31 03:00:00 16.2
2016-12-31 03:30:00 16.3
2016-12-31 04:00:00 16.3
2016-12-31 04:30:00 16.3
Length: 81, dtype: float64