調用交通 api 并使用 python 獲取錯誤格式的數據。#!/usr/bin/env python# make sure to install these packages before running:# pip install pandas# pip install sodapyimport pandas as pdfrom sodapy import Socrata# Unauthenticated client only works with public data sets. Note 'None'# in place of application token, and no username or password:client = Socrata("data.pa.gov", None)# Example authenticated client (needed for non-public datasets):# client = Socrata(data.pa.gov,# MyAppToken,# userame="[email protected]",# password="AFakePassword")# First 2000 results, returned as JSON from API / converted to Python list of# dictionaries by sodapy.results = client.get("dc5b-gebx", limit=50000)# Convert to pandas DataFrameresults_df = pd.DataFrame.from_records(results)results_df.latitude 出來是這樣的latitude0 40 36:56.627這顯然是不正確的,假設這是由于 api 調用的處理方式造成的?還有另一個 location_1 列,它有這樣的字符串數據。 location_1 0 {'latitude': '40.6157', 'longitude': '-75.4621'} 1 {'latitude': '40.4587', 'longitude': '-79.9985'} 2 {'latitude': '39.9328', 'longitude': '-75.2891'} 3 {'latitude': '40.4435', 'longitude': '-80.0046'} 4 {'latitude': '40.5994', 'longitude': '-75.4703'}I need the lat and lon as separate columns對于最好的方法超級困惑,目前我感到很奇怪,我正在考慮簡單地像這樣處理數據框, list(df.location_1.values)然后循環遍歷內部值, dict = {} n = 0 for x in list: n+=1 append(x.strip())
1 回答

精慕HU
TA貢獻1845條經驗 獲得超8個贊
嘗試這個:
df["location_1"] = df["location_1"].apply(lambda x : dict(eval(x))) df2 = df['location_1'].apply(pd.Series)
df2 將包含您的 lang 和 lat。然后您可以合并或連接 df2 到 df
添加回答
舉報
0/150
提交
取消