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.'
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With