熊猫将数据框的列表连接到一个数据框

时间:2018-09-23 18:42:20

标签: python pandas dataframe pandas-groupby

尝试将数据框列表组合为一个数据框。数据如下:

    Date        station_id  Hour    Temp
0   2004-01-01  1           1       46.0
1   2004-01-01  1           2       46.0
2   2004-01-01  1           3       45.0
3   2004-01-01  1           4       41.0
...
433730  2008-06-30  11      3       64.0
433731  2008-06-30  11      4       64.0
433732  2008-06-30  11      5       64.0
433733  2008-06-30  11      6       64.0

这给了我一个数据帧列表:

stations = [x for _,x in df.groupby('station_id')] 

当我重置“ stations”和concat的索引时,我可以得到一个数据框,但它看起来不像我想要的:

for i in range(0,11):
     stations[i].reset_index(drop=True,inplace=True)    

pd.concat(stations,axis=1)

    Date        station_id  Hour    Temp    Date        station_id  Hour    Temp
0   2004-01-01  1           1       46.0    2004-01-01  2           1       38.0
1   2004-01-01  1           2       46.0    2004-01-01  2           2       36.0
2   2004-01-01  1           3       45.0    2004-01-01  2           3       35.0
3   2004-01-01  1           4       41.0    2004-01-01  2           4       30.0

我更愿意成为这样的df:

    Date        Hour    Stn1    Stn2
0   2004-01-01  1       46.0    38.0
1   2004-01-01  2       46.0    6.0
2   2004-01-01  3       45.0    35.0
3   2004-01-01  4       41.0    30.0

我该怎么做?

2 个答案:

答案 0 :(得分:0)

根据您的预期输出,您正在寻找带有.Close { height: 30px; width: 80px; position: relative; } .CloseButton { position: absolute; right: 0; top: 3px; font-size: 20px; color: lightgrey; transition: 1.5s; cursor: pointer; z-index: 1; border-radius: 50%; background-color: white; } .CloseButton:hover { color: black; } .CloseTag { position: absolute; width: 0; height: auto; padding-top: 2px; padding-bottom: 2px; text-indent: 2px; font-size: 16px; background-color: lightgrey; text-align: left; border-radius: 10px 0 0 10px; transition: 1.5s; overflow: hidden; right: 8px; } .CloseButton:hover+.CloseTag { width: 60px; }的{​​{3}}。演示:

<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<div class="Close">
		<a class="CloseButton far fa-times-circle"></a>
		<div class="CloseTag">Close</div>
</div>

答案 1 :(得分:0)

对于它的价值,这到达了我想要去的地方。

stations = [x for _,x in df.groupby('station_id')] #,as_index=True)]
for i in range(0,11):
stations[i].reset_index(drop=True,inplace=True)
stations[i].rename(columns={'Temp':'Stn'+str(i+1)},inplace=True) 
stations[i].drop(columns='station_id',inplace=True)
if i>0:
    stations[i].drop(columns=['Date','Hour'],inplace=True)
stations = pd.concat(stations,axis=1)

不过,对我来说有点蛮力。欢迎其他pythonic建议。