Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将多个数据帧分组为一个,一列作为colname,另一列作为索引?_Python 3.x_Pandas - Fatal编程技术网

Python 3.x 将多个数据帧分组为一个,一列作为colname,另一列作为索引?

Python 3.x 将多个数据帧分组为一个,一列作为colname,另一列作为索引?,python-3.x,pandas,Python 3.x,Pandas,我有一个数据帧列表,每个数据帧的格式如下: id time prob 392 13619782 2018-02-11 21:30:42 0.149796 393 12926892 2018-02-11 21:30:42 0.025866 394 12585081 2018-02-11 21:30:42 0.031470 395 10445552 2018-02-11 21:30:42 0.511263 396

我有一个数据帧列表,每个数据帧的格式如下:

           id                 time      prob
392  13619782  2018-02-11 21:30:42  0.149796
393  12926892  2018-02-11 21:30:42  0.025866
394  12585081  2018-02-11 21:30:42  0.031470
395  10445552  2018-02-11 21:30:42  0.511263
396    360761  2018-02-11 21:30:42  0.030153
397  17162003  2018-02-11 21:30:42  0.068670
398  11387266  2018-02-11 21:30:42  0.182782
时间列在每个数据帧内没有变化,id列在不同的数据帧之间是相同的列表,即下一个数据帧如下所示:

           id                 time      prob
413  13619782  2018-02-11 21:30:57  0.171445
414  12926892  2018-02-11 21:30:57  0.024932
415  12585081  2018-02-11 21:30:57  0.030125
416  10445552  2018-02-11 21:30:57  0.504263
417    360761  2018-02-11 21:30:57  0.027320
418  17162003  2018-02-11 21:30:57  0.062276
419  11387266  2018-02-11 21:30:57  0.179639
我想要的是一个新的数据帧,其中每个数据帧表示一行,其唯一的时间值作为行的索引,id列的值作为列名。例如:

               time  13619782  12585081  12585081  10445552    360761  17162003  11387266
2018-02-11 21:30:42  0.149796  0.025866  0.031470  0.511263  0.030153  0.068670  0.182782
2018-02-11 21:30:57  0.171445  0.024932  0.030125  0.504263  0.027320  0.062276  0.179639

在发布之前,我确实搜索过类似的问题,但没有找到任何问题,但如果之前有人问过/回答过,我深表歉意。我对pandas/data analysis比较陌生,甚至不确定这个过程会被调用什么,所以不确定除了“分组/合并数据帧”之外使用什么搜索词。

在将两个数据帧(假设
df1
df2
)与
append
组合后,您可以尝试
pivot
():

result_df = df1.append(df2, ignore_index=True).pivot(index='time', columns='id', values='prob')
result_df
输出:

id                   360761    10445552  11387266  12585081  12926892  \
time                                                                    
2018-02-11 21:30:42  0.030153  0.511263  0.182782  0.031470  0.025866   
2018-02-11 21:30:57  0.027320  0.504263  0.179639  0.030125  0.024932   

id                   13619782  17162003  
time                                     
2018-02-11 21:30:42  0.149796  0.068670  
2018-02-11 21:30:57  0.171445  0.062276