stefanodv
stefanodv

Reputation: 533

python - Convert float into integer in a column pandas

I have a pandas dataframe as:

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

and I must have:

    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

So I have to transform float to integer only on 'T.'

Upvotes: 3

Views: 4628

Answers (3)

U13-Forward
U13-Forward

Reputation: 71610

Or why not:

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

And now:

print(df3)

Is gonna be expected output:

    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

Upvotes: 0

Dani Mesejo
Dani Mesejo

Reputation: 61930

Using the same idea of @jezrael but with 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)

Output

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

Or using numpy.where with numpy.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)

Upvotes: 0

jezrael
jezrael

Reputation: 863741

It is possible, but a bit hack, because is necessary converting to 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]

If possible missing values:

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]

Upvotes: 5

Related Questions