coelidonum
coelidonum

Reputation: 543

Add comma in a column with integers at a specified position (Pandas)

I have a pandas dataframe like this:

lat      | lon 
___________________
1234444  | 4302134123
134445445| 455463456

I want to separate with a comma the column lat after the third digit, while the column long after the second.

lat      | lon 
___________________
123,4444  | 43,02134123
134,445445| 45,5463456

What's the easist way to do it in pandas?

Upvotes: 0

Views: 253

Answers (1)

David Erickson
David Erickson

Reputation: 16683

you can divide into two strings with .str[i:j] and add comma at the specified position:

df['lat'] = df['lat'].astype(str)
df['lon'] = df['lon'].astype(str)
df['lat'] = df['lat'].str[0:3] + ',' + df['lat'].str[3:]
df['lon'] = df['lon'].str[0:2] + ',' + df['lon'].str[2:]
df
Out[1]: 
          lat          lon
0    123,4444  43,02134123
1  134,445445   45,5463456

Upvotes: 1

Related Questions