x = DataFrame([(1,2.0),(2,4.0)], columns=['a','b'])
x.dtypes
a int64
b float64
a = x.irow(0)
a
output:
a 1.0
b 2.0
Name: 0, dtype: float64
为什么pandas会自动转换为float?使用 irow 后,如何将 int 列保留为 int,而不对每个字段使用 astype/cast?
最佳答案
它返回 Series
对于该行,所以 dtype
晋升为float
由于 float
的存在在山口b
In [33]:
type(x.irow(0))
Out[33]:
pandas.core.series.Series
该方法已被弃用,您应该使用 iloc
:
In [31]:
x.iloc[0]
Out[31]:
a 1.0
b 2.0
Name: 0, dtype: float64
实际上,这并不是一个真正的问题,因为访问行的特定列将保留 dtype
:
In [36]:
x['a'].iloc[0]
Out[36]:
1
In [37]:
type(x['a'].iloc[0])
Out[37]:
numpy.int64
关于python - Pandas irow() 自动将 int 转换为 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37456634/