I have a pandas dataframe of names as shown.
data = {'firstname': {0: 'Sassa', 1: 'Jennifer', 2: 'Jennifer', 3: 'Jennifer', 4: 'Vanessa', 5: 'Alexander', 6: 'Alexander', 7: 'Alexander'}, 'othername': {0: nan, 1: nan, 2: nan, 3: nan, 4: nan, 5: 'Stuart', 6: 'Stuart', 7: 'Stuart'}, 'surname': {0: 'Radomirovic', 1: 'Catto', 2: 'Catto', 3: 'Catto', 4: 'Pinho', 5: 'Clark', 6: 'Clark', 7: 'Clark'}}
df = pd.DataFrame(data)
print(df)
firstname othername surname
0 Sassa NaN Radomirovic
1 Jennifer NaN Catto
2 Jennifer NaN Catto
3 Jennifer NaN Catto
4 Vanessa NaN Pinho
5 Alexander Stuart Clark
6 Alexander Stuart Clark
7 Alexander Stuart Clark
I want to convert these columns to consist of a single-column text name for each person. i.e.
Sasa Radomirovic
Jennifer Catto
Vanessa Pinho
Alexander Stuart Clark
I tried using
personname = df['firstname']+str(' ')+df['othernames'].fillna('')+dfLinks2['surname']
df['personname'] = personname
Problem is, if the person has a middle-name (not NA), this gives no space between middle-name and surname, e.g. Alexander StuartClark. Whereas if I add another str(' ') then those with NA in middle-names end up with two spaces e.g. Jennifer Catto which I don't want.
I also get a SettingWithCopyWarning on the second step.
How should I do this?

df['othernames']str(' ')then those with NA in middle-names end up with two spaces e.g.Jennifer Cattowhich I don't want.str.join()df['PersonName'] = (df["first"]+" "+df["other"].fillna("@")+" "+df["surname"]).str.replace("@ ","").