简体   繁体   中英

Pandas: How to merge columns containing the same name within a single data frame?

I have a dataframe extracted from an excel file which I have manipulated to be in the following form (there are mutliple rows but this is reduced to make my question as clear as possible):

        |A|B|C|A|B|C|
index 0: 1 2 3 4 5 6

As you can see there are repetitions of the column names. I would like to merge this dataframe to look like the following:

        |A|B|C|
index 0: 1 2 3 
index 1: 4 5 6

I have tried to use the melt function but have not had any success thus far.

import pandas as pd
df = pd.DataFrame([[1,2,3,4,5,6]], columns = ['A', 'B','C','A', 'B','C'])
df
       A  B  C  A  B  C
    0  1  2  3  4  5  6
pd.concat(x for _, x in df.groupby(df.columns.duplicated(), axis=1))
       A  B  C
    0  1  2  3
    0  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