简体   繁体   中英

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):

    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))
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:

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 :

>>> 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])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM