[英]concatenate multiple rows to one single row in pandas
I have the following data (this is just a small part, there are 21 rows in actual data):我有以下数据(这只是一小部分,实际数据有21行):
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:我会通过
values
下拉到numpy
,将其reshape
为一行,然后从中创建一个新框架:
>>> 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)". .reshape(1, -1)
基本上意味着“根据需要重新.reshape(1, -1)
为 1 行和尽可能多的列 (-1)”。
The same applies when you want to create one column with many rows:当您想要创建具有多行的一列时,这同样适用:
>>> pd.DataFrame(df.values.reshape(-1, 1))
df = df.stack().to_frame().T
df.columns = list(range(len(df.columns)))
or或者
df = pd.DataFrame(df.stack().to_frame().values).T
will give you:会给你:
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
You could use pandas melt
then you won't need to call pd.DataFrame
for that:您可以使用熊猫
melt
然后您就不需要为此调用pd.DataFrame
:
In [1470]: pd.melt(df, var_name='var', value_name='0').drop('var', axis=1).T
Out[1470]:
0 1 2 3 4 5 6 \
0 33.142857 40.142857 41.714286 52.714286 66.857143 67.142857 0.031429
7 8 9 10 11 12 13 14
0 0.28 0.001429 114 172.714286 179.714286 0.102857 0.192857 0.191429
If you don't really need a dataframe, you can use numpy.array.flatten
:如果你真的不需要数据
numpy.array.flatten
,你可以使用numpy.array.flatten
:
>>> d = pandas.DataFrame([[1, 2], [3, 4], [5, 6]])
>>> d
0 1
0 1 2
1 3 4
2 5 6
>>> d.as_matrix().flatten()
array([1, 2, 3, 4, 5, 6])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.