简体   繁体   English

用特定术语替换单元格

[英]Replace cells with specific terms

I want to replace the words contains "conference" and "group" with "N/A" in the dataframe.我想在数据框中用"N/A"替换包含"conference""group"的词。 Eg "AAAI Conference"->"N/A" "Alibaba Group" -> "N/A"例如“AAAI会议”->“N/A”“阿里巴巴集团”->“N/A”

The dataframe is called name , I try two ways to do this:数据框称为name ,我尝试两种方法来做到这一点:

columns=['nameCurrentEmployer',
       'name2ndEmployer', 'name3rdEmployer',
       'name4thEmployer', 'name5thEmployer',
       'name6thEmployer', 'name7thEmployer',
       'name8thEmployer', 'name9thEmployer',
       'name10thEmployer'] 
name.loc[name.str.contains(['conference','group'], case=False), columns] = 'N/A'

Prompt error AttributeError: 'DataFrame' object has no attribute 'str'提示错误AttributeError: 'DataFrame' object has no attribute 'str'

NAMES = pd.Series(name.values.flatten())
NAMES.loc[NAMES.str.contains(['conference','group'], case=False), columns] = 'N/A'

Now the error is现在的错误是

TypeError: unhashable type: 'list'

Thank you very much.非常感谢。

str.contains() takes str.contains()需要

Character sequence or regular expression.字符序列或正则表达式。


So instead of ['conference','group'] you should use 'conference|group' :因此['conference','group']您应该使用'conference|group'而不是['conference','group']

NAMES.loc[NAMES.str.contains('conference|group', case=False), columns] = 'N/A'

Alternatively, I would suggest to use either apply() :或者,我建议使用apply()

NAMES.name = NAMES.name.apply(lambda x: 'N/A' if 'conference' in x else x)

or str.replace()str.replace()

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

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