How to iterate over values in a column pandas

70 Views Asked by At

I'm working on a project that needs one set of coordinates converted to another(from CRS 229 to CRS 3857), and I have a function that works when you put the former coordinates in, but I would like to make it so that I can just run the function over the entire column(s)(there are X, Y, and a XY column).

This is what the code looks like

from pyproj import Proj, transform, CRS,  Transformer
import pandas as pd
import geopandas as gpd
crs_2229 = CRS.from_epsg(2229)
crs_3857 = CRS.from_epsg(3857)
transformer = Transformer.from_crs(crs_2229, crs_3857)

data = {'X': [6481427.400, 6485873.280],
        'Y': [1797333.200, 1796834.811],
        'XY':[(6481427.400000006,1797333.200000003),(6485873.279835222,1796834.811359349)]}

df = pd.DataFrame(data)

Now if I run this

transformer.transform(6481427.400000006,1797333.200000003)

It comes with the correct transformation of (-13165144.971136427, 4019524.5726486626). But how do I make it a little more automated?

Tried a lambda function, but it kept on rising as a key value error. Tried to make a for loop, but that didn't give me anything. Searched online for functions, but they didn't work on it, and now with the google colabs AI tool, it gave me the same code I would try as suggestions but they never worked.

2

There are 2 best solutions below

2
user19077881 On

You could use:

def func(r):
    return transformer.transform(r[0], r[1])

df['XY_t'] = df['XY'].apply(func)

print(df['XY_t'])

which gives for new column:

0     (-13165144.971136427, 4019524.572648665)
1    (-13163513.038113914, 4019345.0284381276)
0
George.M On

If you're already using geopandas, could you not use a GeoDataFrame and use .to_crs.

from pyproj import CRS
import geopandas as gpd


crs_2229 = CRS.from_epsg(2229) # Can be replaced with a string 'epsg:2229'
crs_3857 = CRS.from_epsg(3857) # Can be replaced with a string 'epsg:3857'

data = {'geometry': gpd.points_from_xy(
(6481427.400000006,1797333.200000003),
(6485873.279835222,1796834.811359349))}

gdf = gpd.GeoDataFrame(data, crs=crs_2229).to_crs(crs_3857)

if you need to get coordinates independently later you could access the x and y as a property or using a function, eg:

gdf.get_coordinates()
gdf['geometry'].x
gdf['geometry'].y