string pattern matching pandas 1

string pattern matching pandas

#df is a data frame and emails is a column.
emails = df.loc[:, 'EMAIL']
# wanted to filter the data from emais column where email id has gmail in it
gmail_emails = df[emails.str.contains('@gmail')]

#For more information:
https://kanoki.org/2019/11/12/how-to-use-regex-in-pandas/

Here is what the above code is Doing:
1. We are using the loc function to select the EMAIL column from the dataframe.
2. We are using the str.contains function to filter the dataframe based on the condition that the email column contains the string ‘@gmail’.
3. We are storing the filtered dataframe in a new variable called gmail_emails.

Similar Posts