gpt4 book ai didi

python - Pandas read_excel : nan values forcing others in the same column to be converted to float

转载 作者:太空宇宙 更新时间:2023-11-04 04:55:14 26 4
gpt4 key购买 nike

假设我要读取以下 Excel 文件:

enter image description here

我想要的是一个简单的解决方案(最好是一行),它可以读取 excel,以便将日期转换为 str(或至少 int), 空白值是 nannat 或任何可以被 pd.isnull 检测到的值。

如果我使用df = pd.read_excel(file_path),我得到的是

df
Out[8]:
001002.XY 600123.AB 123456.YZ 555555.GO
ipo_date 20100203.0 20150605 NaN 20090501.0
delist_date NaN 20170801 NaN NaN

因此 pandas 将空白单元格识别为 NaN,这很好,但让人恼火的是所有其他值都被强制为 float64,即使它们是为了只是 strint。 (编辑:似乎如果列,例如 [1] 列没有 nan,那么其他值不会被迫 float 。但是,在我的例子中,大多数列的 delist_date 都是空白的,因为大多数股票都有 ipo 日期,但尚未退市。)

据我所知,我尝试了 dtype=str 关键字 arg,它给了我

df
Out[10]:
001002.XY 600123.AB 123456.YZ 555555.GO
ipo_date 20100203 20150605 nan 20090501
delist_date nan 20170801 nan nan

好看吗?没错,日期现在是 str,但有一点很荒谬,nan 现在变成了文字字符串!例如

df.iloc[1, 0]
Out[12]:
'nan'

这会让我不得不在稍后添加一些奇怪的东西,比如 df.replace

我没有尝试使用转换器,因为它需要逐列指定数据类型,而我正在使用的实际 excel 文件是一个非常长的电子表格(大约 3k 列)。我也不想在 Excel 本身中转置电子表格。

有人能帮忙吗?提前致谢。

最佳答案

使用 dtype=object 作为参数。

这里有很好的解释:pandas distinction between str and object types

关于python - Pandas read_excel : nan values forcing others in the same column to be converted to float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47234997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com