2

i have the following frame:

col1 col1 

null  hi
hello null

I am trying to merge the two columns as they have the same name:

col1
hi
hello

I am using:

result= result.groupby(result.columns, axis=1).first()

But the first() applies it to the whole column so i get:

col1
hi
null

Anyone to merge the columns and avoid the null values.

skim8201
  • 81
  • 4

1 Answers1

1

Your solution working nice if null are converted to NaNs.

So maybe problem is null are sometimes strings, so first replace.

df = df.replace('null', np.nan).groupby(df.columns, axis=1).first()
print (df)
    col1
0     hi
1  hello
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252