Home > Software engineering >  Combining two columns with the same name in a df
Combining two columns with the same name in a df

Time:07-25

I have two Id columns in my df which are split into either a Id or NaN.

Date ... ID ID
12/12/2019 ... DE1253 NaN
12/12/2018 ... eg562r NaN
12/12/2021 ... gse233 NaN
12/12/2019 ... NaN wefg32
12/11/2010 ... NaN rte422
12/10/2021 ... NaN 3fdes4

How can I combine these two fields into one ignores the NaN so that there the desired output is

Date ... ID
12/12/2019 ... DE1253
12/12/2018 ... eg562r
12/12/2021 ... gse233
12/12/2019 ... wefg32
12/11/2010 ... rte422
12/10/2021 ... 3fdes4

Any help would be much appreciated thanks

CodePudding user response:

Just try with groupby

out = df.groupby(level=0,axis=1).first()

CodePudding user response:

If the columns to be merge have identical names and non overlapping data:

out = df.stack().unstack()

Or, to keep the original order of the columns:

df.stack().unstack()[df.columns.unique()]

output:

         Date  ...      ID
0  12/12/2019  ...  DE1253
1  12/12/2018  ...  eg562r
2  12/12/2021  ...  gse233
3  12/12/2019  ...  wefg32
4  12/11/2010  ...  rte422
5  12/10/2021  ...  3fdes4
  • Related