Pandas: Why is default column type for numeric float?

前端 未结 3 1022
执笔经年
执笔经年 2021-01-02 10:11

I am using Pandas 0.18.1 with python 2.7.x. I have an empty dataframe that I read first. I see that the types of these columns are object which is OK. When I as

3条回答
  •  旧巷少年郎
    2021-01-02 10:39

    The why is almost certainly to do with flexibility and speed. Just because Pandas has only seen an integer in that column so far doesn't mean that you're not going to try to add a float later, which would require Pandas to go back and change the type for all that column. A float is the most robust/flexible numeric type.

    There's no global way to override that behaviour (that I'm aware of), but you can use the astype method to modify an individual DataFrame.

    http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.astype.html

提交回复
热议问题