Skip to content
Advertisement

Find a substring in cells across multiple columns in a Pandas dataframe

I have a large DataFrame with 50+ columns which I’m simplifying here below:

JavaScript

enter image description here

I’m trying to find

a) whether there are any instances of ‘—>’ in any of the cells across the DataFrame?

b) if so where? (optional)

So far I’ve tried 2 approaches

JavaScript

this only works for strings not substrings

JavaScript

I get:

JavaScript

(I believe this may only work for columns with string types)

Would appreciate any help. Open to other approaches as well.

Advertisement

Answer

You can use .applymap() to test each individual value in a dataframe.

JavaScript

To use the .str accessor you can:

JavaScript

The output differs a little – note the Age column is not there.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement