pandas - 将UTM函数应用于dataframe列

时间:2021-08-12 21:27:18

I'm working with this python package called UTM, which converts WGS84 coordinates to UTM and vice versa. I would like to apply this function to a pandas dataframe. The function works as follows:

我正在使用这个名为UTM的python包,它将WGS84坐标转换为UTM,反之亦然。我想将此函数应用于pandas数据帧。该功能的工作原理如下:

utm.from_latlon(51.2, 7.5)
>>> (395201.3103811303, 5673135.241182375, 32, 'U')

where the input is a couple of coordinates, and it returns a tuple of the same coordinates in UTM system. For my purposes I'm interested only in the first two elements of the tuple.

其中输入是几个坐标,并返回UTM系统中相同坐标的元组。出于我的目的,我只对元组的前两个元素感兴趣。

I'm working on a Dataframe called cities like:

我正在研究一个名为city的Dataframe:

City;Latitude;Longitude;minx;maxx;miny;maxy
Roma;41.892916;12.48252;11.27447419;13.69056581;40.99359439;42.79223761
Paris;48.856614;2.352222;0.985506011;3.718937989;47.95729239;49.75593561
Barcelona;41.385064;2.173403;0.974836927;3.371969073;40.48574239;42.28438561
Berlin;52.519171;13.406091;11.92835553;14.88382647;51.61984939;53.41849261
Moscow;55.755826;37.6173;36.01941671;39.21518329;54.85650439;56.65514761

and I would like to add four columns for each row called 'utmminx','utmmax','utmminy','utmmaxy' as a result of applying the utm function to the 'minx','maxx','miny','maxy' columns. So far I tried the following, assigning the first and the second value of the resulting tuple to the new columns:

我想为每行添加四列,称为'utmminx','utmmax','utmminy','utmmaxy',因为将utm函数应用于'minx','maxx','miny',' maxy'专栏。到目前为止,我尝试了以下操作,将生成的元组的第一个和第二个值分配给新列:

cities['utmminx'],cities['utmmaxx'] = utm.from_latlon(cities['minx'],cities['maxx'])[0],utm.from_latlon(cities['minx'],cities['maxx'])[1]

but I received a ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). I tried to set only the first row value to the function and it works:

但是我收到了一个ValueError:系列的真值是模棱两可的。使用a.empty,a.bool(),a.item(),a.any()或a.all()。我试图只将第一行值设置为函数,它可以工作:

utm.from_latlon(cities['minx'][0],cities['maxx'][0])[0],utm.from_latlon(cities['minx'][0],cities['maxx'][0])[1]
>>> (357074.7837193568, 1246647.7959235134)

I would like to avoid classical loops over the dataframe as I thought there is a classical pandas method to do this.

我想避免数据帧上的经典循环,因为我认为有一个经典的pandas方法来做到这一点。

2 个解决方案

#1


Starting with your frame

从您的框架开始

        City   Latitude  Longitude       minx       maxx       miny       maxy
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594  42.792238
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292  49.755936
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742  42.284386
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849  53.418493
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504  56.655148

We define a function that takes a row, calls utm.from_latlon() and returns a Series of the first two elements of the tuple that we get from utm. Then we use Pandas' apply() to call that function. I just did one set of coordinates, but you can do the same apply() statement for the others.

我们定义一个接受一行的函数,调用utm.from_latlon()并返回我们从utm获得的元组的前两个元素的系列。然后我们使用Pandas的apply()来调用该函数。我只做了一组坐标,但你可以为其他坐标执行相同的apply()语句。

EDIT I changed the function to index by position instead of name to make the function reusable

编辑我将函数更改为按位置而不是名称索引,以使函数可重用

def getUTMs(row):
    tup = utm.from_latlon(row.ix[0],row.ix[1])
    return pd.Series(tup[:2])

cities[['utmminy','utmminx']] = cities[['miny','maxx']].apply(getUTMs , axis=1)
cities

       City   Latitude  Longitude       minx       maxx       miny  \
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594   
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292   
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742   
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849   
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504   

        maxy        utmminy         utmminx  
0  42.792238  389862.562124  4538871.624816  
1  49.755936  553673.645924  5311803.556837  
2  42.284386  531525.080929  4481738.581782  
3  53.418493  491957.246518  5718764.545758  
4  56.655148  513814.029424  6078844.774914  

#2


You could use apply method over the columns like

你可以在列之上使用apply方法

Using a lambda function

使用lambda函数

In [120]: lambdafunc = lambda x: pd.Series(utm.from_latlon(x['minx'], x['maxx'])[:2])

And, applying row-wise

并且,逐行应用

In [121]: cities[['utmminx', 'utmmax']] = cities.apply(lambdfunc), axis=1)

In [122]: cities
Out[122]:
        City   Latitude  Longitude       minx       maxx       miny       maxy        utmminx          utmmax
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594  42.792238  357074.783719  1246647.795924
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292  49.755936  579990.155575   108936.764630
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742  42.284386  541385.186664   107751.160445
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849  53.418493  487350.117333  1318634.001517
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504  56.655148  519389.217259  3986123.464910

#1


Starting with your frame

从您的框架开始

        City   Latitude  Longitude       minx       maxx       miny       maxy
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594  42.792238
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292  49.755936
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742  42.284386
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849  53.418493
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504  56.655148

We define a function that takes a row, calls utm.from_latlon() and returns a Series of the first two elements of the tuple that we get from utm. Then we use Pandas' apply() to call that function. I just did one set of coordinates, but you can do the same apply() statement for the others.

我们定义一个接受一行的函数,调用utm.from_latlon()并返回我们从utm获得的元组的前两个元素的系列。然后我们使用Pandas的apply()来调用该函数。我只做了一组坐标,但你可以为其他坐标执行相同的apply()语句。

EDIT I changed the function to index by position instead of name to make the function reusable

编辑我将函数更改为按位置而不是名称索引,以使函数可重用

def getUTMs(row):
    tup = utm.from_latlon(row.ix[0],row.ix[1])
    return pd.Series(tup[:2])

cities[['utmminy','utmminx']] = cities[['miny','maxx']].apply(getUTMs , axis=1)
cities

       City   Latitude  Longitude       minx       maxx       miny  \
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594   
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292   
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742   
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849   
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504   

        maxy        utmminy         utmminx  
0  42.792238  389862.562124  4538871.624816  
1  49.755936  553673.645924  5311803.556837  
2  42.284386  531525.080929  4481738.581782  
3  53.418493  491957.246518  5718764.545758  
4  56.655148  513814.029424  6078844.774914  

#2


You could use apply method over the columns like

你可以在列之上使用apply方法

Using a lambda function

使用lambda函数

In [120]: lambdafunc = lambda x: pd.Series(utm.from_latlon(x['minx'], x['maxx'])[:2])

And, applying row-wise

并且,逐行应用

In [121]: cities[['utmminx', 'utmmax']] = cities.apply(lambdfunc), axis=1)

In [122]: cities
Out[122]:
        City   Latitude  Longitude       minx       maxx       miny       maxy        utmminx          utmmax
0       Roma  41.892916  12.482520  11.274474  13.690566  40.993594  42.792238  357074.783719  1246647.795924
1      Paris  48.856614   2.352222   0.985506   3.718938  47.957292  49.755936  579990.155575   108936.764630
2  Barcelona  41.385064   2.173403   0.974837   3.371969  40.485742  42.284386  541385.186664   107751.160445
3     Berlin  52.519171  13.406091  11.928356  14.883826  51.619849  53.418493  487350.117333  1318634.001517
4     Moscow  55.755826  37.617300  36.019417  39.215183  54.856504  56.655148  519389.217259  3986123.464910