-1

I want to replace the words contains "conference" and "group" with "N/A" in the dataframe. E.g. "AAAI Conference"->"N/A" "Alibaba Group" -> "N/A"

The dataframe is called name, I try two ways to do this:

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'

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.

4
  • i'd suggest you use pandas str replace instead and possibly use a regex expression containing the words 'conference' or 'group' Commented Feb 3, 2020 at 22:35
  • What are you using the string 'N/A' for? Why are you doing pd.Series(name.values.flatten()) ? Can you share more of your program? Variable and function names should follow the lower_case_with_underscores style. Always share the entire error message. Do you not have a minimal reproducible example? Commented Feb 3, 2020 at 22:54
  • Also, is this not just a worse duplicate of stackoverflow.com/questions/39602824/… ? Commented Feb 4, 2020 at 0:26
  • @ sammywemmy Thank you, str replace works. Commented Feb 4, 2020 at 3:54

1 Answer 1

0

str.contains() takes

Character sequence or regular expression.


So instead of ['conference','group'] you should use 'conference|group':

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

Alternatively, I would suggest to use either apply():

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

or str.replace()

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, Giorgos. But it still prompts two errors: 1.TypeError: 'Series' objects are mutable, thus they cannot be hashed 2.Indexing Error How should I fix it or is there any other way that can do the same job? Thanks again.
Why not recommend the use of DataFrame.replace, instead of the needlessly awkward .loc[] method?
@RenzhiZhao Can you address the points made by the first commenter and myself?
@ AMC Thank you, AMC. Replace works. You really help me out and give me a good lesson about raising good questions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.