Danny David Leybzon Danny David Leybzon - 2 months ago 21
Python Question

How do I concat corresponding values of two Pandas series?

I have a PD DF with three columns: lon, lat, and count:

lon lat count
123 456 3
789 012 4
345 678 5


And I'd like to concat lon and lat to make a fourth column so that the df looks like:

lon lat count text
123 456 3 '123, 456'
789 012 4 '789, 012'
345 678 5 '345, 678'


I tried:

grouped['text'] = pd.Series([str(grouped['lon'])]).str.cat([str(grouped['lat'])], sep=', ')


But it appears to return:

lon lat text
123 456 '123, 456', '345, 678', '789, 012'
789 012
345 678


What am I missing here?

Answer
df['lon', 'lat'].astype(str).apply(lambda r: ', '.join(r), axis=1)

Turn your integers into strings so that you can use them in join, then apply the join function for each row (i.e. across each row cell thus axis=1)

Another way:

df.astype(str).lon+', '+df.astype(str).lat  # astype optional if df already as strings