1 Upvote

Using EndsWith, StartsWith & Contains to Filter DataFrame Columns

#Only keep rows where the text_column string ends with 'xy'
df = df[df['text_column'].str.endswith('xy')]

#Only keep rows where the text_column string starts with 'test'
df = df[df['text_column'].str.startswith('test')]

#Only keep rows where the text_column string contains 'ID'
df = df[df['text_column'].str.contains('ID')]

By GregHe1979 - Last Updated Feb. 19, 2022, 9:15 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3
2
Top Contributors
103
100