亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python字符串列表,無法正確格式化

Python字符串列表,無法正確格式化

萬千封印 2023-08-08 17:20:57
調用交通 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


查看完整回答
反對 回復 2023-08-08
  • 1 回答
  • 0 關注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號