【发布时间】:2021-07-13 16:38:05
【问题描述】:
我正在使用数据框,我必须将一列转换为 int 类型
我使用以下符号:
result_df['ftmSectionId'] = result_df['ftmSectionId'].astype('int')
DF 有几百万行,所以显然有些值无法转换为 int(可能包括逗号或句点...)我收到错误:
ValueError: invalid literal for int() with base 10: 'not'
现在根据这个问题: How do I fix invalid literal for int() with base 10 error in pandas
我可以使用:
data.Population1 = pd.to_numeric(data.Population1, errors="coerce")
哪个有效。
但是以这种方式,我不知道为什么我首先会出错。 由于我正在使用的数据库的性质,我希望该特定列只有整数。 如何使用简单的方法 .astype('int') 查询列以找出哪些值不能转换为 'int'?
谢谢
其他可能但不重复的答案: Unable to convert pandas dataframe column to int variable type using .astype(int) method 这个问题解决了同样的问题,只是他们知道问题是该列包含 NaN 并且他们删除了它们。我不知道这里有什么问题,我的目标不仅是转换为 'int' 而是抓住麻烦值
【问题讨论】: