N9909
N9909

Reputation: 247

convert pandas dataframe datatypes from float64 into int64

I am trying to read CSV file by using python pandas, in the resultant dataframe one column is returned as float64 datatype instead of int64. But I could see most of the values are numbers and some of them are null values in the existing CSV file

df = pd.read_csv(file)

dh.head(3)

Name State  Id
SFO  CA     123.0
JFK  NY     152.0
CHG  IL     NaN
ABC  AZ     NaN

df.dypes

Name Object
State Object
Id float64

I tried convert Id column into Int64 to upload data into oracle table

df['Id'] = df['Id'].astype('int64')

Error : Cannot convert NA to integer

Is there any approach to convert Id column into int64 ? I appreciate your response.

Upvotes: 1

Views: 2197

Answers (2)

Charts
Charts

Reputation: 11

in your code

df['Id'] = df['Id'].astype('int64')

for astype('Int64') in code, try to use cap letter 'I', not small letter 'i', Like

df['Id'] = df['Id'].astype('Int64')

Upvotes: 1

Chicodelarose
Chicodelarose

Reputation: 882

In Python 3.7.6 and pandas 1.0.3 you can do:

df['Id'] = df['Id'].astype(pd.Int64Dtype())

print(df.dtypes)
print(df)

Output:

Name     object
State    object
Id        Int64

State    Id
0  SFO    CA   123
1  JFK    NY   152
2  CHG    IL  <NA>
3  ABC    AZ  <NA>

Upvotes: 1

Related Questions