python-将pandas列中的float转换为整数

时间:2018-12-13 09:18:21

标签: python pandas dataframe floating-point integer

我的熊猫数据框为:

df3 = pd.DataFrame({
'T': [11.0,22.0,11.23,20.03],
'v2': [11.0,13.0,55.1,33.0],
'v3' : [112.1,2.0,2.1,366.0],
'v4': [np.nan, "blue", 1.0, 2.0]
 })

       T    v2     v3    v4
0  11.00  11.0  112.1   NaN
1  22.00  13.0    2.0  blue
2  11.23  55.1    2.1   1.0
3  20.03  33.0  366.0   2.0

我必须有:

    T       v2     v3    v4
0  11     11.0  112.1   NaN
1  22     13.0    2.0  blue
2  11.23  55.1    2.1   1.0
3  20.03  33.0  366.0   2.0

所以我只能在'T'上将float转换为整数。

3 个答案:

答案 0 :(得分:5)

有可能,但有点hack,因为有必要转换为object

df3['T'] = np.array([int(x) if int(x) == x else x for x in df3['T']], dtype=object)
print (df3)
       T    v2     v3    v4
0     11  11.0  112.1   NaN
1     22  13.0    2.0  blue
2  11.23  55.1    2.1     1
3  20.03  33.0  366.0     2

print (df3['T'].tolist())
[11, 22, 11.23, 20.03]

如果可能缺少值:

df3 = pd.DataFrame({
'T': [11.0,22.0,11.23,np.nan],
'v2': [11.0,13.0,55.1,33.0],
'v3' : [112.1,2.0,2.1,366.0],
'v4': [np.nan, "blue", 1.0, 2.0]
 })


df3['T'] = np.array([int(x) if x % 1 == 0 else x for x in df3['T']], dtype=object)
print (df3)
       T    v2     v3    v4
0     11  11.0  112.1   NaN
1     22  13.0    2.0  blue
2  11.23  55.1    2.1     1
3    NaN  33.0  366.0     2

print (df3['T'].tolist())
[11, 22, 11.23, nan]

答案 1 :(得分:0)

使用与@jezrael相同的想法,但使用is_integer

import numpy as np
import pandas as pd

df3 = pd.DataFrame({
    'T': [11.0, 22.0, 11.23, 20.03],
    'v2': [11.0, 13.0, 55.1, 33.0],
    'v3': [112.1, 2.0, 2.1, 366.0],
    'v4': [np.nan, "blue", 1.0, 2.0]
})

df3['T'] = np.array([int(x) if float(x).is_integer() else x for x in df3['T']], dtype=object)

print(df3)

输出

T    v2     v3    v4
0     11  11.0  112.1   NaN
1     22  13.0    2.0  blue
2  11.23  55.1    2.1     1
3  20.03  33.0  366.0     2

或将numpy.wherenumpy.fmod一起使用:

mask = np.fmod(df3['T'].values, 1) == 0
df3['T'] = np.where(mask, df3['T'].values.astype(np.int), df3['T']).astype(dtype=object)
print(df3)

答案 2 :(得分:0)

或者为什么不呢?

df3=df3.apply(lambda x: int(x) if int(x)==x and x==x and isinstance(x,float) else x)

现在:

print(df3)

将被期望输出:

    T       v2     v3    v4
0  11     11.0  112.1   NaN
1  22     13.0    2.0  blue
2  11.23  55.1    2.1   1.0
3  20.03  33.0  366.0   2.0