Home > Net >  Concat two columns and keep unique values if repeating
Concat two columns and keep unique values if repeating

Time:10-18

I have a dataframe as follows:

df = pd.DataFrame({'foodstuff':['apple-martini', 'apple-pie', None, 'dessert', None], 'type':[None, None, 'strawberry-tart', 'dessert', None]})

df
Out[10]:
foodstuff           type
0   apple-martini   None
1   apple-pie       None
2   None            strawberry-tart
3   dessert         dessert
4   None            None

I want to achieve the following:

df
Out[10]:
Combined
0   apple-martini   
1   apple-pie       
2   strawberry-tart
3   dessert         
4   None            

The solution here tackles the case of combining the columns when one of the columns is definitely None. I am trying to achieve the case that if there is repeat of values in a row for the two columns, then only one value is retained.

CodePudding user response:

We can just do fillna

df['combine'] = df.foodstuff.fillna(df.type)
0      apple-martini
1          apple-pie
2    strawberry-tart
3            dessert
4               None
Name: foodstuff, dtype: object

CodePudding user response:

You can use combine_first:

df['combined'] = df['foodstuff'].combine_first(df['type'])
print(df)

# Output:
       foodstuff             type         combined
0  apple-martini             None    apple-martini
1      apple-pie             None        apple-pie
2           None  strawberry-tart  strawberry-tart
3        dessert          dessert          dessert
4           None             None             None
  • Related