Search for String in all Pandas DataFrame columns and filter

horatio1701d

Thought this would be straight forward but had some trouble tracking down an elegant way to search all columns in a dataframe at same time for a partial string match. Basically how would I apply df['col1'].str.contains('^') to an entire dataframe at once and filter down to any rows that have records containing the match?

unutbu

The Series.str.contains method expects a regex pattern (by default), not a literal string. Therefore str.contains("^") matches the beginning of any string. Since every string has a beginning, everything matches. Instead use str.contains("\^") to match the literal ^ character.

To check every column, you could use for col in df to iterate through the column names, and then call str.contains on each column:

mask = np.column_stack([df[col].str.contains(r"\^", na=False) for col in df])
df.loc[mask.any(axis=1)]

Alternatively, you could pass regex=False to str.contains to make the test use the Python in operator; but (in general) using regex is faster.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas DataFrame filter by rows and columns

From Dev

Pandas filter columns of a DataFrame with bool

From Dev

Filter dataframe by two columns in Pandas

From Dev

Concatenate all columns in a pandas dataframe

From Dev

How to filter all dataframe columns to an condition in Pyspark?

From Dev

Search and filter pandas dataframe with regular expressions

From Dev

Pandas - filter and regex search the index of DataFrame

From Dev

Filter pandas dataframe based on values in multiple columns

From Dev

How to filter pandas dataframe columns by partial label

From Dev

Fastest way to filter a pandas dataframe on multiple columns

From Dev

Filter Pandas dataframe based on combination of two columns

From Dev

Filter a pandas dataframe based on two columns

From Dev

Fastest way to filter a pandas dataframe on multiple columns

From Dev

How to filter Pandas rows by another Dataframe columns?

From Dev

how to search for a string in all table Columns in android?

From Dev

Mysql query search a string in all columns of a table

From Dev

Search string in all varchar2 columns

From Dev

Search for a string in all table columns in database sql

From Java

How to show all of columns name on pandas dataframe?

From Java

pandas how to check dtype for all columns in a dataframe?

From Dev

Not calculating sum for all columns in pandas dataframe

From Dev

Pandas: Find the maximum range in all the columns of dataframe

From Dev

pandas select rows by condition for all of dataframe columns

From Dev

How to show all of columns name on pandas dataframe?

From Dev

pandas how to check dtype for all columns in a dataframe?

From Dev

select range of values for all columns in pandas dataframe

From Dev

Not calculating sum for all columns in pandas dataframe

From Dev

Normalize pandas dataframe with all columns together

From Dev

Trying to select data from all columns that start with string from a pandas dataframe

Related Related

  1. 1

    pandas DataFrame filter by rows and columns

  2. 2

    Pandas filter columns of a DataFrame with bool

  3. 3

    Filter dataframe by two columns in Pandas

  4. 4

    Concatenate all columns in a pandas dataframe

  5. 5

    How to filter all dataframe columns to an condition in Pyspark?

  6. 6

    Search and filter pandas dataframe with regular expressions

  7. 7

    Pandas - filter and regex search the index of DataFrame

  8. 8

    Filter pandas dataframe based on values in multiple columns

  9. 9

    How to filter pandas dataframe columns by partial label

  10. 10

    Fastest way to filter a pandas dataframe on multiple columns

  11. 11

    Filter Pandas dataframe based on combination of two columns

  12. 12

    Filter a pandas dataframe based on two columns

  13. 13

    Fastest way to filter a pandas dataframe on multiple columns

  14. 14

    How to filter Pandas rows by another Dataframe columns?

  15. 15

    how to search for a string in all table Columns in android?

  16. 16

    Mysql query search a string in all columns of a table

  17. 17

    Search string in all varchar2 columns

  18. 18

    Search for a string in all table columns in database sql

  19. 19

    How to show all of columns name on pandas dataframe?

  20. 20

    pandas how to check dtype for all columns in a dataframe?

  21. 21

    Not calculating sum for all columns in pandas dataframe

  22. 22

    Pandas: Find the maximum range in all the columns of dataframe

  23. 23

    pandas select rows by condition for all of dataframe columns

  24. 24

    How to show all of columns name on pandas dataframe?

  25. 25

    pandas how to check dtype for all columns in a dataframe?

  26. 26

    select range of values for all columns in pandas dataframe

  27. 27

    Not calculating sum for all columns in pandas dataframe

  28. 28

    Normalize pandas dataframe with all columns together

  29. 29

    Trying to select data from all columns that start with string from a pandas dataframe

HotTag

Archive