1

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.

2

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

5 Comments

I have tried, but no luck.Its giving error AttributeError: Module 'pandas' has no attribute 'pd.Int64Dtype()'
What is your python and pandas version?
python - 3.6 and pandas - 0.19.2
@Naresh I added my Python and pandas version to my answer. Good luck!
@Naresh You -at least- will have to upgrade to pandas 0.24.2 for this to work pandas.pydata.org/pandas-docs/version/0.24/whatsnew/…
1

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')

1 Comment

the "int64" literal is valid and doesn't need to be capitalized, best case they can invoke the 'ignore' argument: pandas.pydata.org/docs/reference/api/… ... or as someone suggested use a pd.Int64Dtype() function which handles NaN differently ... keep contributing and best regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.