使用python将多个附加值转换为熊猫数据框

问题描述 投票:1回答:1

这里我有一些附加值和日期。我想将这些数据转换成熊猫。但是我不知道该怎么做。

这是我的代码,用于读取附加值:在此代码之前,我编写了另一个代码来附加值。之后,我想将4个值读为4列。

x1.append({'date':next_record_time, 'x1':new_x1)
x2.append({'date':next_record_time, 'x2':new_x2})
x3.append({'date':next_record_time, 'x3':new_x3})
x4.append({'date':next_record_time, 'x4':new_x4})
pd.DataFrame(x1, columns=['date', 'x1'])

然后值

    date                              x1
2018-06-08 09:15:00 		 500                             
2018-06-08 10:15:00 		 812.0015321149272
2018-06-08 11:15:00 		 1256.691444199051
2018-06-08 12:15:00 		 784.1793119716488
2018-06-08 13:15:00 		 489.3302935759677
2018-06-08 14:15:00 		 805.343602611898
2018-06-08 15:15:00 		 502.5368757877344

    date                               x2
2018-06-08 15:00:00 		 2
2018-06-08 16:00:00 		 1.8711387084858075
2018-06-08 17:00:00 		 1.7505800331969679
2018-06-08 18:00:00 		 1.6377890312086083
2018-06-08 19:00:00 		 1.5322652263139487
2018-06-08 20:00:00 		 1.4335403883113977
2018-06-08 21:00:00 		 1.3411764553736159

  date                               x3
2018-06-08 09:16:00 		 50
2018-06-08 10:16:00 		 35.357940964363564
2018-06-08 11:16:00 		 25.003679784788382
2018-06-08 12:16:00 		 17.681572674447967
2018-06-08 13:16:00 		 12.503680055604704
2018-06-08 14:16:00 		 58.842087624867226
2018-06-08 15:16:00 		 41.61070120919926

     date                             x4
2018-06-08 09:15:00 		 40
2018-06-08 10:15:00 		 37.75543557660705
2018-06-08 11:15:00 		 35.63682288948313
2018-06-08 12:15:00 		 33.63709426897089
2018-06-08 13:15:00 		 31.749578641409713
2018-06-08 14:15:00 		 29.967979274504085
2018-06-08 15:15:00 		 28.286352771490854

您可以看到这是我的四个附加值

然后,我需要将所有日期插入一列,并将x1,x2,x3,x4读为四列。我期望的输出是:

date                    x1                     x2             x3            x4
2018-06-08 09:15:00     500                     0              0            40
2018-06-08 09:16:00      0                      0              50            0  
2018-06-08 10:15:00   812.0015321149272         0              0            37.75543557660705 
2018-06-08 10:16:00      0                      0        35.357940964363564  0 
2018-06-08 11:15:00   1256.691444199051         0              0            35.63682288948313
2018-06-08 11:16:00      0                      0        25.003679784788382  0   
2018-06-08 12:15:00   784.1793119716488         0              0             33.63709426897089
2018-06-08 12:16:00      0                      0        17.681572674447967  0
2018-06-08 13:15:00   489.3302935759677         0              0             31.74957864140971
2018-06-08 13:16:00      0                      0        12.503680055604704  0
2018-06-08 14:15:00   805.343602611898          0              0             29.967979274504085
2018-06-08 14:16:00      0                      0        58.842087624867226  0
2018-06-08 15:00:00      0                      2             0              0
2018-06-08 15:15:00   502.5368757877344         0             0         28.286352771490854
2018-06-08 15:16:00      0                      0        1.8711387084858075  0   

转换为熊猫后,我只有一个数据值。这是我得到的:

      	  date               x1
0 	2018-06-08 09:15:00 	40.000000
1 	2018-06-08 10:15:00 	37.755436
2 	2018-06-08 11:15:00 	35.636823
3 	2018-06-08 12:15:00 	33.637094
4 	2018-06-08 13:15:00 	31.749579
5 	2018-06-08 14:15:00 	29.967979

有人可以帮我解决这个问题吗?

pandas numpy dataframe date append
1个回答
1
投票

IIUC,

[首先,将所有词典传递到列表中,然后我们将它们作为一个数据帧读取,然后我们可以将其合并并堆叠以获取所需的结果。

data_dicts = x1 + x2 + x3 + x4

df = pd.DataFrame.from_dict(data_dicts)
df['date'] = df['date'].apply(pd.to_datetime)

s = pd.melt(df,id_vars='date')
final = s.groupby(['date','variable'])['value'].first().unstack().reset_index().fillna(0)
del final.columns.name

结果:

print(final)

                  date           x1        x2         x3         x4
0  2018-06-08 09:15:00   500.000000  0.000000   0.000000  40.000000
1  2018-06-08 09:16:00     0.000000  0.000000  50.000000   0.000000
2  2018-06-08 10:15:00   812.001532  0.000000   0.000000  37.755436
3  2018-06-08 10:16:00     0.000000  0.000000  35.357941   0.000000
4  2018-06-08 11:15:00  1256.691444  0.000000   0.000000  35.636823
5  2018-06-08 11:16:00     0.000000  0.000000  25.003680   0.000000
6  2018-06-08 12:15:00   784.179312  0.000000   0.000000  33.637094
7  2018-06-08 12:16:00     0.000000  0.000000  17.681573   0.000000
8  2018-06-08 13:15:00   489.330294  0.000000   0.000000  31.749579
9  2018-06-08 13:16:00     0.000000  0.000000  12.503680   0.000000
10 2018-06-08 14:15:00   805.343603  0.000000   0.000000  29.967979
11 2018-06-08 14:16:00     0.000000  0.000000  58.842088   0.000000
12 2018-06-08 15:00:00     0.000000  2.000000   0.000000   0.000000
13 2018-06-08 15:15:00   502.536876  0.000000   0.000000  28.286353
14 2018-06-08 15:16:00     0.000000  0.000000  41.610701   0.000000
15 2018-06-08 16:00:00     0.000000  1.871139   0.000000   0.000000
16 2018-06-08 17:00:00     0.000000  1.750580   0.000000   0.000000
17 2018-06-08 18:00:00     0.000000  1.637789   0.000000   0.000000
18 2018-06-08 19:00:00     0.000000  1.532265   0.000000   0.000000
19 2018-06-08 20:00:00     0.000000  1.433540   0.000000   0.000000
20 2018-06-08 21:00:00     0.000000  1.341176   0.000000   0.000000
© www.soinside.com 2019 - 2024. All rights reserved.