Select Rows For Specific Column Value

I want a list of row numbers based on the product name I requested.

import pandas as pd
pn=[('Samsung Galaxy phone case', '28'), ('MacBook Air case', '30'), ('iPhone 11', '1500'), ('red nail polish', '10'), ('Silver Samsung Galaxy A10', '800'), ('HP monitor', '500'), ('iPhone 11 case', '20')]
df=pd.DataFrame(pn, columns=['Product Name', 'Price'])

I want to get a list of row names that contain ‘case’
The desired outcome should look like:

1 Samsung Galaxy phone case 28
2 MacBook Air case 30
7 iPhone 11 case 20

I have tried the code below, but it doesn’t work

#1
case=df.loc['case']
print(case)
#2
case=df[df['Product Name']=='case']
print(case)

Do you know what function I can use to find what I want?

Hi,

note that the pn variable includes a list of tuples. Each tuple listed has an index of 0 and 1. With this knowledge in hand, we can iterate through each tuple in the list and perform a search. We also know where to search, the first item of each tuple. Note that by default, each tuple and list begins with an index of 0. Thus, this is where we’ll be doing our search for the word case.

for index, string in enumerate(pn):
    if 'case' in string[0]:
        print(str(index + 1) + ' ' + string[0] + ' ' + string[1])

After running this script, you get:

1 Samsung Galaxy phone case 28
2 MacBook Air case 30
7 iPhone 11 case 20

… per your requirements above.

This should work:

df[df['Product Name'].str.contains('case')]

Result:

                Product Name Price
0  Samsung Galaxy phone case    28
1           MacBook Air case    30
6             iPhone 11 case    20