gpt4 book ai didi

python - 我如何处理这种情况 : 'n/a' shows up as 'nan' in pandas dataframe, 但无法对其进行字符串匹配并替换

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

我有一个 CSV 文件,其中有几行是 n/a。当我将它作为 Pandas 数据框加载时,它显示为 nan

当我在这几行中使用 splitlower 等函数时,这会导致问题。

data_df['column'][104]
>>> nan

data_df['column'][104].split()
>>>
AttributeError Traceback (most recent call last)
<ipython-input-38-6efe06f0a4ec> in <module>()
----> 1 data_df['column'][104].split()

AttributeError: 'float' object has no attribute 'split'

data_df['column'][104].lower()
>>>
AttributeError Traceback (most recent call last)
<ipython-input-41-c80cc9ae0712> in <module>()
----> 1 data_df['column'][104].lower()

AttributeError: 'float' object has no attribute 'lower'

当我尝试用 fillna 方法用空格替换 nan 时(这不会导致这些错误),它没有做任何事情:

data_df.fillna('')

data_df['column'][104]
>>> nan

所以我尝试将其替换为字符串:

for i in range(len(data_df)):
if data_df['column'][i]=='nan':
data_df['column'][i]=''

data_df['column'][104]
>>> nan

for i in range(len(data_df)):
if data_df['column'][i]=='n/a':
data_df['column'][i]=''

data_df['column'][104]
>>> nan

以下不打印任何内容:

for i in range(len(data_df)):
if (data_df['column'][i]=='nan' or data_df['column'][i]=='n/a'):
print(data_df['column'][i])

为什么我无法捕获和替换 nann/a?我该如何解决?

最佳答案

我认为我们可以在一开始就修复它

df=pd.read_csv('Yourfile.csv',na_values=['n/a']).fillna('')

关于python - 我如何处理这种情况 : 'n/a' shows up as 'nan' in pandas dataframe, 但无法对其进行字符串匹配并替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51237537/

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