調(diào)用交通 api 并使用 python 獲取錯(cuò)誤格式的數(shù)據(jù)。#!/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="user@example.com",# 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 出來(lái)是這樣的latitude0 40 36:56.627這顯然是不正確的,假設(shè)這是由于 api 調(diào)用的處理方式造成的?還有另一個(gè) location_1 列,它有這樣的字符串?dāng)?shù)據(jù)。 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對(duì)于最好的方法超級(jí)困惑,目前我感到很奇怪,我正在考慮簡(jiǎn)單地像這樣處理數(shù)據(jù)框, list(df.location_1.values)然后循環(huán)遍歷內(nèi)部值, dict = {} n = 0 for x in list: n+=1 append(x.strip())
1 回答

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