Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to override for() to get geographic coordinates from a pandas dataframe?

I have the following dataframe:

      import pandas as pd

      df = pd.DataFrame({'City': ['Paris', 'New York', 'Rio'],
               
               'Point': [(48.853638186045075, 2.3164768734228094, 0.0),
                         (40.73149967161843, -73.99345738955843, 0.0),
                         (-22.925268779593164, -43.23729165751779, 0.0)]
               })

      print(df)

      # Output:

      City               Point
      Paris             (48.853638186045075, 2.3164768734228094, 0.0)
      New York          (40.73149967161843, -73.99345738955843, 0, 0)
      Rio               (-22.925268779593164, -43.23729165751779, 0.0)

I need to separate the geographic coordinates from latitude and longitude. So I made the following code:

      df['lat'] = 0
      df['long'] = 0

      for i in range(0, len(df)):
         df['lat'].iloc[i] = df['Point'][i][0]
         df['long'].iloc[i] = df['Point'][i][1]

      print(df)

      # Output:

      City               Point                                      lat          long
      Paris       (48.853638186045075, 2.3164768734228094, 0.0)    48.853638     2.316477
      New York    (40.73149967161843, -73.99345738955843, 0, 0)    40.731500    -73.993457
      Rio         (-22.925268779593164, -43.23729165751779, 0.0)   -22.925269   -43.237292

The implementation is working perfectly. However, I would like to remove the for() to make the operation more efficient. How can I remove for()?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

cols = ['lat','long']
df[cols] = df['Point'].apply(lambda p: pd.Series([p[0],p[1]]),index=cols)

Explanation:
Convert each Point tuple into a Series, the result of the apply method is a DataFrame. Assign the result as new columns to df.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading