如何向此数据框添加其他列以将坐标转换为邮政编码?

时间:2017-12-07 15:51:33

标签: python python-3.x

所以我试图在我的数据框中添加一个额外的列,因为它已经有了坐标,但我需要邮政编码。

import pandas as pd
from geopy.geocoders import Nominatim

url3 = 'https://raw.githubusercontent.com/chrisc617/Data-Bootcamp'
url4 = '-Final-Project/master/NYC_Transit_Subway_Entrance_And_Exit_Data.csv'
url5 = url3+url4
trains = pd.read_csv(url5)
trains.columns

trains['Zip Code'] = (location = geolocator.reverse(trains['Station Location'])
print(location.address[-18:-13]))

以上代码不是最佳方式吗?

1 个答案:

答案 0 :(得分:1)

这个怎么样:

trains['Zip Code'] = trains['Station Location'].apply(lambda x: geolocator.reverse(x))