在 pandas 中将多行连接到一行 [英] concatenate multiple rows to one single row in pandas
问题描述
我有以下数据(这只是一小部分,实际数据有21行):
wt_tmin wt_tmax wt_prec wt_sol_rad wt_ET0 33.142857 52.714286 0.031429 114.000000 0.1028571 40.142857 66.857143 0.280000 172.714286 0.1928572 41.714286 67.142857 0.001429 179.714286 0.191429
我想将所有行连接成一行,如下所示:
0 1 2 3 4 5 6 7 8 9 11 12 13 14 1533.142857 52.714286 0.031429 114.000000 0.102857 40.142857 66.857143 0.280000 172.714286 .7000000 0.102857 40.142857 66.857143 0.280000 172.714286 0.714286 .7000000 0.102859 20.7 19167879 7000000 0.19167879
这是我尝试做的:
# K 是包含数据的 Pandas 数据框KE = pd.concat([K.icol(0), K.icol(1), K.icol(2), K.icol(3), K.icol(4)],axis=1).T
但这并没有给我想要的结果.请帮忙
I'd drop down to numpy
via values
, reshape
it到一行,然后从中创建一个新框架:
.reshape(1, -1)
基本上意味着根据需要将形状调整为 1 行和尽可能多的列 (-1)".
当您想创建具有多行的一列时,同样适用:
<预><代码>>>>pd.DataFrame(df.values.reshape(-1, 1))I have the following data (this is just a small part, there are 21 rows in actual data):
wt_tmin wt_tmax wt_prec wt_sol_rad wt_ET
0 33.142857 52.714286 0.031429 114.000000 0.102857
1 40.142857 66.857143 0.280000 172.714286 0.192857
2 41.714286 67.142857 0.001429 179.714286 0.191429
I want to concatenate all the rows to one row like as follows:
0 1 2 3 4 5 6 7 8 9 11 12 13 14 15
33.142857 52.714286 0.031429 114.000000 0.102857 40.142857 66.857143 0.280000 172.714286 0.192857 41.714286 67.142857 0.001429 179.714286 0.191429
Here's what I tried to do:
# K is a pandas dataframe with the data
KE = pd.concat([K.icol(0), K.icol(1), K.icol(2), K.icol(3), K.icol(4)], axis=1).T
but this does not give me the desired result. Please help
I'd drop down to numpy
via values
, reshape
it to one row, and then make a new frame from that:
>>> pd.DataFrame(df.values.reshape(1, -1))
0 1 2 3 4 5 6 7 \
0 33.142857 52.714286 0.031429 114 0.102857 40.142857 66.857143 0.28
8 9 10 11 12 13 14
0 172.714286 0.192857 41.714286 67.142857 0.001429 179.714286 0.191429
.reshape(1, -1)
basically means "reshape to 1 row and as many columns as necessary (-1)".
The same applies when you want to create one column with many rows:
>>> pd.DataFrame(df.values.reshape(-1, 1))
这篇关于在 pandas 中将多行连接到一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!