我试图从一列中删除除字母和空格以外的所有字符,但当我使用代码来执行同样的操作时,它给出的输出是
'nan'
,而不是
NaN
(空值)。
数据输入。
ABC ad YQW \2 AQ4 GHExpected output:
ABC ad AQ GH我一直在使用的代码。
df['col1'] = df['col1'].astype(str).str.extract(r'([A-Za-z]+(?: [A-Za-z]+)*)')
后来我用这一列来检查NaN
的条件,但它没有给出,因为在执行上述脚本后,它把NaN
的值改为'nan'
。
注意:如果不通过.astype(str)
将其转换为字符串,我的数据将得到
AttributeError:只能对字符串值使用.str访问器!