# Replace the column with the converted values
df['mixed_types'] = pd.to_numeric(df['mixed_types'], errors='coerce')
# Drop NA values, listing the converted columns explicitly
# so NA values in other columns aren't dropped
df.dropna(subset = ['mixed_types'])
Out[11]:
mixed_types
0 12331.0
1 345.0
Pandas有一些工具可以转换这些类型的列,但它们可能并不完全适合您的需要。
pd.to_numeric
像您这样转换混合列,但将非数字字符串转换为NaN
。这意味着您将得到浮点列,而不是整数,因为只有浮点列可以有NaN
值。这通常不太重要,但值得注意。如果要删除所有
NaN
行:您可以直接使用df.u get_numeric_data()。
可以使用^{} 和
errors=coerce
来用NaN
替换非数值,并将其应用于每一列。然后你可以使用dropna
或fillna
任何你喜欢的。相关问题 更多 >
编程相关推荐