简体   繁体   中英

How can i stack pandas dataframes with different column names vertically

I have 2 dataframes that looks like this:

Index1   Games1
   1        1   
   2        5
   3        10
Index2   Games2
   4        2   
   5        4
   6        6

How can I combine them to make it like this:

Index   Games
   1        1   
   2        5
   3        10
   4        2   
   5        4
   6        6

Thank you!

Try this:

import pandas as pd
import numpy

# Assuming your dataframes are named df1, and df2

new_frame = pd.DataFrame(numpy.vstack((df1.values, df2.values)))

print(new_frame)

This method creates a new dataframe by performing the vstack operation out of the numpy library.

Vstack is essentially a way of concatenating, but stacks them in sequence, preserving their row order.

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