gpt4 book ai didi

python - Pandas 将 NULL 读取为 NaN float 而不是 str

转载 作者:太空狗 更新时间:2023-10-30 02:26:40 28 4
gpt4 key购买 nike

<分区>

给定文件:

$ cat test.csv 
a,b,c,NULL,d
e,f,g,h,i
j,k,l,m,n

第 3 列将被视为 str

当我在列上执行字符串函数时,pandas 已将 NULL str 读取为 NaN float :

>>> import pandas as pd
>>> df = pd.read_csv('test.csv', names=[0,1,2,3,4], dtype={0:str, 1:str, 2:str, 3:str, 4:str})

>>> df[3].apply(str.strip)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.5/site-packages/pandas/core/series.py", line 2355, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src/inference.pyx", line 1569, in pandas._libs.lib.map_infer (pandas/_libs/lib.c:66440)
TypeError: descriptor 'strip' requires a 'str' object but received a 'float'

验证:

>>> for i in df[3]:
... print (type(i), i)
...
<class 'float'> nan
<class 'str'> h
<class 'str'> m

我在初始化时指定了 dtype 但不知何故它被覆盖了。

如何强制固定特定列的类型?

有没有办法自动找到这些异常的NaN float ,然后变回'NULL'字符串?

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