从数据框中删除小数

2024-04-26 20:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的数字

24.00

2.00

3.00

我想要一个

24 

2 

3

我使用过.astype(int),round(),但我一直使用前者。我怎样才能让它工作


Tags: 数字introundastype
2条回答

您可以通过将pandasoptionprecision设置为0来解决这个问题

import pandas as pd
df = pd.DataFrame(data={"col": [24.00, 2.00, 3.00]})
print(df)
    col
0  24.0
1   2.0
2   3.0
pd.set_option('precision',0)
print(df)
   col
0   24
1    2
2    3

您需要重新分配dataframe (我想你的错误是):

>>> import pandas as pd
>>> df = pd.DataFrame(data={"col": [24.00, 2.00, 3.00]})
>>> df.dtypes
col    float64
dtype: object
>>> df
    col
0  24.0
1   2.0
2   3.0
>>> df=df.astype(int)
>>> df
   col
0   24
1    2
2    3
>>> df.dtypes
col    int32
dtype: object

相关问题 更多 >