Rupesh Mohan
Rupesh Mohan

Reputation: 41

some float values are not converting to int in pandas Dataframe

df['movieId']=df['movieId'].astype('int')
df.loc[92]['movieId']

The output from df.loc[92]['movieId'] is 134368.0 But it should be 134368

I checked the dtype

df.loc[92]['movieId'].dtype

and it gives dtype('float64') but the other values have been converted to int.

Upvotes: 4

Views: 109

Answers (2)

jezrael
jezrael

Reputation: 863301

Problem is in selecting:

df.loc[92]['movieId']

First select 92 index, what convert row to Series with floats, because some another float column(s).

Correct selecting is by DataFrame.loc with index and column value:

df.loc[92, 'movieId']

Sample:

df = pd.DataFrame({'movieId':[1343680.0, 134369.0],
                   'col':[4,7.5]}, index=[92,95])
print (df)
    col    movieId
92  4.0  1343680.0
95  7.5   134369.0

df['movieId']=df['movieId'].astype('int')
print (df)
    col  movieId
92  4.0  1343680
95  7.5   134369

print (df.loc[92])
col              4.0
movieId    1343680.0
Name: 92, dtype: float64

print (df.loc[92]['movieId'])
1343680.0

print (df.loc[92, 'movieId'])
1343680

Upvotes: 2

Rakesh
Rakesh

Reputation: 82785

Try using numpys int64

Ex:

import numpy as np
df['movieId']=df['movieId'].astype(np.int64)

Upvotes: 1

Related Questions