简体   繁体   English

pandas.read_excel 列作为具有 NA 值的 int64

[英]pandas.read_excel column as int64 with NA values

I'm trying to import an excel spreadsheet onto pandas using:我正在尝试使用以下方法将 excel 电子表格导入到 Pandas 中:

df= pd.read_excel(excel_file)

This reads integer columns fine as int64, as long as they don't have missing or nan values.只要它们没有缺失值或 nan 值,这会将整数列读取为 int64。 When they do have nan values it is read as float64.当它们确实有 nan 值时,它被读取为 float64。

I have tried using:我试过使用:

df= pd.read_excel(excel_file, converters={'column_x': np.int64, 'column_y': np.int64})

and

df= pd.read_excel(excel_file, dtype={'column_x': np.int64, 'column_y': np.int64})

I'd like to keep the missing values as nan but the other values as int.我想将缺失值保留为 nan,而将其他值保留为 int。 Is this possible?这可能吗?

Thanks谢谢

是的,在带有Nullable 整数数据类型的Pandas 0.24+ 中:

df= pd.read_excel(excel_file, dtype={'column_x': 'Int64', 'column_y': 'Int64'})

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

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