[英]CSV using '-' as NULL. Error to convert column to INT
我有一個 CSV
df = pd.read_csv('data.csv')
桌子:
A列 | B列 | 專欄C |
---|---|---|
4068744 | -1472525 | 2596219 |
198366 | - | - |
該文件對 nul 值使用“-”
我嘗試在不處理“-”的情況下轉換為 int。
我的問題是:如何在不更改負值的情況下去除字符串“-”?
df['Column B'] = df['Column B'].astype(int)
ValueError:以 10 為底的 int() 的無效文字:'-'
更高版本的pandas
可以容納integer
具有缺失值的數據類型。 普通int
轉換不支持 null 值。
# replace - with null
df.replace('-', pd.NA, inplace=True)
# and use Int surrounding with ''
df['Column B'] = df['Column B'].astype('Int64')
output:
> df
Column A Column B Column C
0 4068744 -1472525 2596219
1 198366 <NA> <NA>
> df['Column B'].info
Name: Column B, dtype: Int64>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.