import pandas as pd data = pd.Series(['Apple', 'banana', 'Cherry', 'date']) result = data.str.contains('a') print(result.tolist())
The method str.contains('a') checks if the lowercase letter 'a' is in each string. 'Apple' has uppercase 'A' so it returns False, but since 'Apple' starts with uppercase 'A', it does not match lowercase 'a'. 'banana' and 'date' contain lowercase 'a', so True. 'Cherry' does not contain 'a'.
df[df['Name'].str.contains('^J.*n$', regex=True)]?import pandas as pd df = pd.DataFrame({'Name': ['John', 'Jason', 'Jordan', 'Joan', 'Jan', 'Jim']}) filtered = df[df['Name'].str.contains('^J.*n$', regex=True)] print(filtered['Name'].tolist())
The regex ^J.*n$ matches strings that start with 'J' and end with 'n'. 'John', 'Jordan', 'Jan' match. 'Jason' and 'Joan' do not end with 'n'. 'Jim' does not.
import pandas as pd data = pd.Series(['cat', 'dog', 'bird']) result = data.str.contains('[a-z') print(result)
The regex pattern [a-z is missing a closing bracket, causing an unbalanced bracket error.
import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'City': ['New York', 'Yorkshire', 'Los Angeles', 'york', 'Boston']}) # Which code below produces the correct bar chart?
Option C uses case=False to match 'York' regardless of case, counting all rows containing 'York' or 'york'. Others are case sensitive and miss some matches.
import pandas as pd df = pd.DataFrame({'Text': ['I love my Cat', 'Dog is friendly', 'Cats and dogs', 'No pets here', 'dog and cat']}) # Which code below produces a DataFrame with counts of rows mentioning 'cat' and 'dog'?
Option B creates a Series with sums (counts of rows containing each animal ignoring case) for 'cat' and 'dog', then converts to a DataFrame with column 'Count'.