Jane Borges
Jane Borges

Reputation: 592

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()?

Upvotes: 1

Views: 95

Answers (1)

tozCSS
tozCSS

Reputation: 6114

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.

Upvotes: 1

Related Questions