有一个包含多列的 csv 文件,有些列混合了字母和数字。需要删除字母并设置为空并将列更改为整数但出现一些错误。似乎 Pandas 最近添加了可为空的整数类型。https://pandas.pydata.org/pandas-docs/stable/user_guide/integer_na.html。但是在更改为 int 时我仍然会出错。我需要将列保留为 int,因此我无法使用另一种解决方法将列设置为在列中使用 NAN 浮动。数据如下所示:
id count volume
001, A , 1
002, 1 , 2
Run Code Online (Sandbox Code Playgroud)
列数和卷包含以下值:“1”、“2”、“A”、.....
我使用 re 模块来删除字母和空格
df["count"] = df["count"].apply(lambda x: re.sub(r'\s[a-zA-Z]*', '',x))
Run Code Online (Sandbox Code Playgroud)
现在列中的值看起来像:'1'、'2'、''、......
尝试更改为“Int64”但出现错误:
df["count"].astype(str).astype('Int64')
Run Code Online (Sandbox Code Playgroud)
类型错误:对象无法转换为 IntegerDtype
任何建议或解决方法?
df['count'] = pd.to_numeric(df['count'], errors='coerce').astype('Int64')
Run Code Online (Sandbox Code Playgroud)