d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df = pd.DataFrame(d)
print df ['one']
输出:
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
该值设置为float
d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two' : pd.Series([1, 2, 3], index=['a', 'b', 'c'])}
df = pd.DataFrame(d)
print df ['one']
输出:
a 1
b 2
c 3
Name: one, dtype: int64
但是现在该值设置为int64.
区别是第一个,值中包含NaN.
在上述示例中,设置数据类型背后的规则是什么?
谢谢!
解决方法:
pandas继承了numpy的许多错误决定.
参考:
如果查看type(df.iloc [3,0]),则可以看到nan的类型为numpy.float64,这迫使整个列的类型强制转换为浮点型.基本上,Pandas处理可空整数是垃圾,您只需要将它们作为浮点数处理即可.如果不关心性能,也可以使用对象类型来保存整数.
标签:pandas,dataframe,python,pandas,numpy
如果觉得《python dataframe数据类型_python-Pandas DataFrame 1 2 3和NaN值的默认数据类型》对你有帮助,请点赞、收藏,并留下你的观点哦!