In [29]: df.iloc[:, -6:] = df.iloc[:, -6:].apply(pd.to_numeric, errors='coerce', downcast='integer')
In [30]: df
Out[30]:
a b c d e f g h
0 john 1 NaN 2.0 2 42.0 3 NaN
1 david 2 28.0 52.0 15 NaN 2 NaN
2 kevin 3 1.0 NaN 1 10.0 1 5.0
In [31]: df.dtypes
Out[31]:
a object
b int64
c float64
d float64
e int8
f float64
g int8
h float64
dtype: object
多亏了MaxU,我用nan=-1添加了这个选项:
http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.to_numeric.html
谢谢@AntonvBR for the ^{} hint :
相关问题 更多 >
编程相关推荐