简体   繁体   中英

what is easiest way to combine 2 columns in pandas dataframe

I have 2 different dataframes.

group1

      Gene Symbol
 1    AA
 2    BB

group2

      Gene Symbol
 1    XX
 2    YY
 3    ZZ

I want to merge this 2 dataframes in 1 column.

new = pd.DataFrame({'group1':group1['Gene Symbol'], 'group2':group2['Gene Symbol']})

I am able to merge dataframes but I couldn't combine columns like this.

      group1     group2   total
 1    AA         XX       AA
 2    BB         YY       BB
 3               ZZ       XX
 4                        YY
 5                        ZZ

I have tried a lots of methods but I didn't find the solution.

concat twice

df=pd.concat([pd.concat([df1,df2],ignore_index=True),
              df1.reset_index(drop=True),
              df2.reset_index(drop=True)],axis=1)
df.columns=['total','g1','g2']
df
Out[349]: 
  total   g1   g2
0    AA   AA   XX
1    BB   BB   YY
2    XX  NaN   ZZ
3    YY  NaN  NaN
4    ZZ  NaN  NaN

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