pandas将多列带时间戳的Series合并为单个DataFrame

背景:
遇到这样的需求:
将多列这样以时间戳为index的Series数据拼接为整体的DataFrame(只带有一列index)
数据举例:
(1) 温度数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2020-03-20 00:00:00    10.911639
2020-03-20 00:30:00 10.561648
2020-03-20 01:00:00 10.410831
2020-03-20 01:30:00 9.993831
2020-03-20 02:00:00 9.996133
2020-03-20 02:30:00 9.807382
2020-03-20 03:00:00 9.831737
2020-03-20 03:30:00 9.767458
2020-03-20 04:00:00 9.789868
2020-03-20 04:30:00 9.993025
2020-03-20 05:00:00 10.203707
2020-03-20 05:30:00 10.460783
2020-03-20 06:00:00 10.900585
2020-03-20 06:30:00 11.154725
2020-03-20 07:00:00 11.765203
2020-03-20 07:30:00 12.377077
2020-03-20 08:00:00 13.013389
2020-03-20 08:30:00 13.685595
2020-03-20 09:00:00 14.327092
2020-03-20 09:30:00 14.862767
2020-03-20 10:00:00 15.264174

(2) 湿度数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2020-03-20 00:00:00    30.071777
2020-03-20 00:30:00 31.718174
2020-03-20 01:00:00 32.415719
2020-03-20 01:30:00 33.542252
2020-03-20 02:00:00 33.725316
2020-03-20 02:30:00 34.507023
2020-03-20 03:00:00 34.402693
2020-03-20 03:30:00 35.050728
2020-03-20 04:00:00 35.082839
2020-03-20 04:30:00 34.889389
2020-03-20 05:00:00 34.481423
2020-03-20 05:30:00 33.377774
2020-03-20 06:00:00 31.306436
2020-03-20 06:30:00 30.868772
2020-03-20 07:00:00 29.368311
2020-03-20 07:30:00 27.515762
2020-03-20 08:00:00 24.488863
2020-03-20 08:30:00 22.346596
2020-03-20 09:00:00 19.126895
2020-03-20 09:30:00 17.111340
2020-03-20 10:00:00 13.799469

解决的问题就是:保留一列时间戳作index,把剩下多列的Series.values拼接在一起。
(1)首先,找一组Series作为第一列,比如温度,先将series转换DataFrame:
self.preData[0]表示Series的温度数据,本文在self.preData列表中存放了5组上面说的Series数据。

1
2
frame = {'Date Time': self.preData[0].index, 'Temperature': self.preData[0].values}
frame = pd.DataFrame(frame) # 转换DataFrame

(2)将第一列时间戳作为index

1
2
frame['Date Time'] = pd.to_datetime(frame['Date Time'])
frame.set_index('Date Time', inplace=True)

(3)按列拼接

1
2
3
4
frame['Humidity'] = self.preData[1].values      # 温度列的DataFrame,与其他列的Series拼接
frame['Wind'] = self.preData[2].values
frame['WindSpeed'] = self.preData[3].values
frame['itPower'] = self.preData[4].values

因为列还算少,可直接指定列名。列太多可能要另想办法,希望对您有帮助~

Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2020 Hu Jinlei
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信