我在这里是新手,理想情况下,我会从我从哪里学到了idxmax的用法来评论这个问题:
我使用了相同的方法,下面是我的代码
df = pd.DataFrame(np.arange(16).reshape(4,4),columns=["A","B","C","D"],index=[0,1,2,3])
一旦df[(df>6)]
在此df上使用,这些int值就会更改为float?
A B C D
0 NaN NaN NaN NaN
1 NaN NaN NaN 7.0
2 8.0 9.0 10.0 11.0
3 12.0 13.0 14.0 15.0
熊猫为什么要这样做?另外,我在某处读到可以在系列上使用dtype = object,但是还有其他避免这种情况的方法吗?
如果您确实想让int看起来像
df.astype(object).mask(df<=6)
Out[114]:
A B C D
0 NaN NaN NaN NaN
1 NaN NaN NaN 7
2 8 9 10 11
3 12 13 14 15
这种权衡主要是出于内存和性能方面的考虑,并且使得最终的Series仍然是“数字”。一种可能性是改用dtype = object数组。
有关更多信息 astype(object)
df.astype(object).mask(df<=6).applymap(type)
Out[115]:
A B C D
0 <class 'float'> <class 'float'> <class 'float'> <class 'float'>
1 <class 'float'> <class 'float'> <class 'float'> <class 'int'>
2 <class 'int'> <class 'int'> <class 'int'> <class 'int'>
3 <class 'int'> <class 'int'> <class 'int'> <class 'int'>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句