简体   繁体   中英

add one row from another dataframe in pandas

Here's the thing, I need to put one row from other dataframe to the top of main dataframe in pandas, above first row where are columns named.

Sample :

       1value  2value 3value 4value 5value
acity    4       3       6      2     6
bcity    2       6       6      4     1
ccity    5       11      53     6     3
dcity    5       1       4      6     3 
gcity    6       4       2      7     4

And the other sample:

1value 2value 3value 4value 5value
 2       5       2      6     3 

And now I need to add value of second sample to the top of first sample. Desired output:

         2       5       2      6     3 
      1value  2value 3value 4value 5value
acity    4       3       6      2     6
bcity    2       6       6      4     1
ccity    5       11      53     6     3
dcity    5       1       4      6     3 
gcity    6       4       2      7     4

And just for mention, I have about 3000 rows, and 250 columns in this Sample dataframe.

I don't have any code yet, I tried to find here something...

Not sure if this is what you need, but a multi index data frame looks like the output:

df1 or second sample :

在此处输入图片说明

df or the first sample :

在此处输入图片说明

Rename the columns with a multi-index columns:

df.columns = pd.MultiIndex.from_arrays([df1.values[0], df.columns])

在此处输入图片说明

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