0

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
ja_him
  • 417
  • 1
  • 4
  • 20

2 Answers2

0

I've used this to combine 2 columns into one, you can create a new column or use an existing column.

df['New Address']=pd.concat([df['Address 2'].dropna(),df['Address 3'].dropna()])
aguay091
  • 52
  • 1
  • 8
0

As per the comment:

df['Address 2'] + df['Address 3']

pretty easy.... (doh)

ja_him
  • 417
  • 1
  • 4
  • 20