从熊猫数据框中删除.0而不将其转换为字符串

时间:2019-12-18 23:06:13

标签: python-3.x pandas

如果我们只需要将104.0转换为104,而无需使用%g会将其转换为字符串。 python中是否有任何方法-pandas,我们可以通过该方法检查列是否为浮点型,并且我不想将其转换为字符串或强制转换为整数,但只能设置为140.0到140且具有139.58到139.58。请帮忙吗?

1 个答案:

答案 0 :(得分:1)

您可以(至少)通过两种方式来做到这一点。

假设我们有一个看起来像这样的系列:

x = pd.Series([140.0, 120.0, 100.0])
x

0    140.0
1    120.0
2    100.0
dtype: float64

然后可以使用 astype()方法:

x = x.astype(int)
x

0    140
1    120
2    100
dtype: int64

对于Python,请记住您具有 int()函数。

x = int(140.0)
x
140