简体   繁体   English

将 Pandas 数据帧数据类型从 float64 转换为 int64

[英]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.我正在尝试使用 python pandas 读取 CSV 文件,在结果数据框中,一列作为 float64 数据类型而不是 int64 返回。 But I could see most of the values are numbers and some of them are null values in the existing CSV file但我可以看到大多数值是数字,其中一些是现有 CSV 文件中的空值

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我尝试将 Id 列转换为 Int64 以将数据上传到 oracle 表

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

Error : Cannot convert NA to integer错误:无法将 NA 转换为整数

Is there any approach to convert Id column into int64 ?有什么方法可以将 Id 列转换为 int64 吗? I appreciate your response.我很感激你的回应。

In Python 3.7.6 and pandas 1.0.3 you can do:Python 3.7.6pandas 1.0.3 中,您可以执行以下操作:

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM