[英]User-defined function to create a dataset
我的任務是使用給定的用戶定義 function 為特定區域創建數據集。
我已經在 function 中插入了我的特定數據
但只得到這個錯誤:
KeyError: "[' magType ' ' nst' ' gap' ' dmin ' ' rms' ' place ' ' type '\n ' horizontalError ' ' depthError ' ' magError ' ' magNst ' ' status '\n ' locationSource ' ' magSource ' ' net' 'id' ' updated '] not found in axis"
我該如何解決這個問題? 因為當我查看我的數據時,我得到了所有這些信息(magType 等)
問題是您從 url 獲取的pd.DataFrame
的列是;
['time', 'latitude', 'longitude', 'depth', 'mag', 'magType', 'nst', 'gap', 'dmin', 'rms', 'net', 'id', 'updated', 'place', 'type', 'horizontalError', 'depthError', 'magError', 'magNst', 'status', 'locationSource', 'magSource']
這與您插入到drop
function 中的列名不匹配。 重新格式化列名[" magType "," nst"," gap"," dmin "," rms"," place "," type "," horizontalError "," depthError "," magError "," magNst "," status "," locationSource "," magSource "," net","id"," updated "]
刪除 function 中的[" magType "," nst"," gap"," dmin "," rms"," place "," type "," horizontalError "," depthError "," magError "," magNst "," status "," locationSource "," magSource "," net","id"," updated "]
與您的pd.DataFrame
object data
完全匹配。
PS:您可能想查看fstrings
或.format
。 這將使您的代碼看起來更干凈。
第二個 PS:您也可能不想遞歸連接data
。 隨着data
變大,這個過程會明顯變慢。 更好的方法是創建一個列表(例如dfList
)和 append 列表,其中包含您從 url 獲取的數據幀。 然后使用 function pd.concat(dfList)
。 干杯!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.