I want to make Address 2
and Address 3
combined into a column. Originally I tried casting to a string and using its concatenate function like this:
df['newAdd2'] = df['Address 2'].astype(str).str.cat(df['Address 3'].astype(str), sep=' ')
(from here: Combine two columns of text in dataframe in pandas/python)
which gave the results like this:
VillageA TownA
nan nan
nan TownC
VillageD nan
I want to make Address 3
added to Address 2
(in a new column is fine). Sometimes Address 3
is missing a value, sometimes Address 2
is missing a value. This if else
process is clunky (and might not even work...). What's a Pythonic way of doing it?
if df['Address 2']:
if df['Address 3']:
#concate 2 and 3
else:
#only show 2
else:
if df['Address 3']:
#only show 3