I wonder if there is a way to join between strings from two columns of a data frame as I referred to this: Combine two columns of text in pandas dataframe But it seems like I cannot find the solution that satisfied my need. Suppose I have a data frame with two columns: First and Second. So the Third column should be sth like this:
df['Third'] = df.First.astype(str).str.cat(df.Second.astype(str), sep=',')
Normally, the concatenation works well when those 2 columns are string but in my case, the first and the second column can sometimes contain NaN
So Is there a way to set the condition on sep whether it needs to put or not in case for example: if df['First']= 'first' and df['Second'] = NaN then df['Third'] = 'first' without a , at the end. if df['first'] = NaN and df['Second'] = 'second' then df['Third'] = 'second' without a , at the beginning of the string.
How can I achieve this? Any help would be much appreciated. Thank you!