相关文章推荐
痴情的红薯  ·  java ...·  3 月前    · 
坏坏的路灯  ·  java如何获取json数组 ...·  1 年前    · 
温暖的金鱼  ·  这可能是 Python ...·  1 年前    · 
长情的豆腐  ·  android - Add space ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I search on all columns (except the first) of my DataFrame and add a new column 'Matching_Columns' with the name of the matching column, When I try to remove all dots before testing if my pattern is contained within a row I receive an error.

This works:

keyword='123456789'
f = lambda row: row.apply(str).str.contains(keyword ,na=False, flags=re.IGNORECASE)
df1 = df.iloc[:,1:].apply(f, axis=1)
df.insert(loc=1, column='Matching_Columns', value=df1.dot(df.columns[1:] + ', ').str.strip(', '))

This gives me an error:

keyword='123456789'
f = lambda row: row.apply(str).str.replace(".","").contains(keyword ,na=False, flags=re.IGNORECASE)
df1 = df.iloc[:,1:].apply(f, axis=1)
df.insert(loc=1, column='Matching_Columns', value=df1.dot(df.columns[1:] + ', ').str.strip(', '))

Error:

AttributeError: ("'Series' object has no attribute 'contains'", 'occurred at index 0')

I can't figure out what's wrong with this. It looks like the str.replace(".","") is causing the error.

Any help would be appreciated

It should be str.contains because contains is an accessor of str not series.

So, to explain this better, str.replace returns a series object. You have injected contains function to series. That's why you are getting the error 'Series' object has no attribute 'contains'

f = lambda row: row.apply(str).str.replace(".","").str.contains(keyword ,na=False, flags=re.IGNORECASE)
                An additional question if that is allowed. Now I'm using a static keyword. Is it possible to use the first column as keyword and test  if my pattern is contained in one of the other columns?
– John Doe
                Jun 15, 2019 at 5:48
                @JohnDoe Use apply. i.e. suppose you have cols text and to_find, then use df.apply(lambda x: x['to_find'] in x['text'], axis=1)
– Vishnudev Krishnadas
                Jun 15, 2019 at 5:56
                .str is an accessor available to Series with the object datatype. All of the available accessors and the dtype restrictions are in that link.
– ALollz
                Jun 15, 2019 at 5:59
                @JohnDoe Now that this question is solved, you may ask another question if you have some issues. The S.O. community will help.
– Vishnudev Krishnadas
                Jun 15, 2019 at 6:03
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.