TransWikia.com

what is easiest way to combine 2 columns in pandas dataframe

Stack Overflow Asked by tyasird on November 12, 2021

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.

One Answer

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

Answered by BENY on November 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP