我有一個1億行的資料集,我需要進行分析。我使用這個函式來讀取檔案:
csv2020=pd.read_csv('filename.txt',
sep=" "。
error_bad_lines=False。
usecols=['field1'/span>, 'field2'/span>, 'field3'/span>, 'field4'/span>]。
dtype={'field1': int,'field2': float, 'field3': float, 'field4': float})
但是我得到了一個錯誤,關于其中一行不可能轉換為浮點數的錯誤:
ValueError
ValueError: could not convert string to float: 'ORCH' 我想省略發生這個錯誤的任何行,但除了error-bad-lines引數外,我不知道該怎么做。幫助嗎? 謝謝! uj5u.com熱心網友回復: 你試圖匯入的一些浮動列有字串,因此不能被轉換。
首先閱讀沒有 "dtype.... "的CSV,然后看一下你的資料框架
error_bad_lines選項不是為了這個目的,它只適用于一個不正確的欄位數量。
在沒有dtype選項的情況下讀取你的檔案,然后使用pandas.to_numeric,使用errors='coerce'選項進行轉換:
df = pd.read_csv(..)
df['field1'] = pd.to_numeric(df['field1'], errors='coerce')
df['field2'] = ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/310245.html
標籤:
上一篇:如何在R中找到一列的中位數
