pandas 在熊猫中将多行连接到一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34641615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
concatenate multiple rows to one single row in pandas
提问by user3119875
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
但这并没有给我想要的结果。请帮忙
回答by DSM
I'd drop down to numpyvia values, reshapeit to one row, and then make a new frame from that:
我会下拉到numpyvia values,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)基本上意味着“根据需要将形状调整为 1 行和尽可能多的列 (-1)”。
回答by Stefan
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
回答by Anton Protopopov
You could use pandas meltthen you won't need to call pd.DataFramefor that:
您可以使用Pandas,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
回答by Paulo Scardine
If you don't really need a dataframe, you can use 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])

