pandas - apply UTM function to dataframe columns

Fabio Lamanna

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.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.

I'm working on a Dataframe called cities like:

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:

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:

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.

Bob Haffner

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.

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  

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Use ternary operator in apply function in pandas dataframe, without grouping columns

From Dev

Can I apply a function to multiple columns in Pandas dataframe without a for loop?

From Dev

pandas apply User defined function to grouped dataframe on multiple columns

From Dev

pandas dataframe group columns based on name and apply a function

From Dev

Apply function on dataframe Column to get several other columns Pandas Python

From Dev

pandas dataframe: groupby by several columns, apply function and map back the result

From Dev

apply function to a DataFrame GroupBy and return fewer columns

From Java

Apply function to all columns in a my dataframe

From Java

R Apply() function on specific dataframe columns

From Dev

Apply a function which operates on specific columns in a dataframe

From Dev

How to apply a function that creates columns to dataframe in R

From Dev

Apply function to a list of columns in a loop and output dataframe

From Dev

R Apply() function on specific dataframe columns

From Dev

How to apply Window function to multiple columns in DataFrame

From Dev

Apply a function in a dataframe's columns [Python]

From Dev

pandas apply function to multiple columns and multiple rows

From Dev

Pandas: How to apply a function to different columns

From Dev

Pandas apply function to groups of columns and indexing

From Dev

Pandas apply rowwise function on multiple columns

From Dev

Pandas: How to apply a function to different columns

From Dev

Apply function to multiple pandas columns with Args

From Dev

Pandas dataframe transform columns to json columns using apply or vectorization

From Dev

Pandas dataframe transform columns to json columns using apply or vectorization

From Dev

Apply function on each column in a pandas dataframe

From Dev

pandas DataFrame, how to apply function to a specific column?

From Dev

Apply custom cumulative function to pandas dataframe

From Dev

How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

From Dev

Pandas dataframe apply function to entire column

From Dev

Apply similar function to multilevel pandas dataframe

Related Related

  1. 1

    Use ternary operator in apply function in pandas dataframe, without grouping columns

  2. 2

    Can I apply a function to multiple columns in Pandas dataframe without a for loop?

  3. 3

    pandas apply User defined function to grouped dataframe on multiple columns

  4. 4

    pandas dataframe group columns based on name and apply a function

  5. 5

    Apply function on dataframe Column to get several other columns Pandas Python

  6. 6

    pandas dataframe: groupby by several columns, apply function and map back the result

  7. 7

    apply function to a DataFrame GroupBy and return fewer columns

  8. 8

    Apply function to all columns in a my dataframe

  9. 9

    R Apply() function on specific dataframe columns

  10. 10

    Apply a function which operates on specific columns in a dataframe

  11. 11

    How to apply a function that creates columns to dataframe in R

  12. 12

    Apply function to a list of columns in a loop and output dataframe

  13. 13

    R Apply() function on specific dataframe columns

  14. 14

    How to apply Window function to multiple columns in DataFrame

  15. 15

    Apply a function in a dataframe's columns [Python]

  16. 16

    pandas apply function to multiple columns and multiple rows

  17. 17

    Pandas: How to apply a function to different columns

  18. 18

    Pandas apply function to groups of columns and indexing

  19. 19

    Pandas apply rowwise function on multiple columns

  20. 20

    Pandas: How to apply a function to different columns

  21. 21

    Apply function to multiple pandas columns with Args

  22. 22

    Pandas dataframe transform columns to json columns using apply or vectorization

  23. 23

    Pandas dataframe transform columns to json columns using apply or vectorization

  24. 24

    Apply function on each column in a pandas dataframe

  25. 25

    pandas DataFrame, how to apply function to a specific column?

  26. 26

    Apply custom cumulative function to pandas dataframe

  27. 27

    How to apply a function ( BigramCollocationFinder) to Pandas DataFrame

  28. 28

    Pandas dataframe apply function to entire column

  29. 29

    Apply similar function to multilevel pandas dataframe

HotTag

Archive