Nini
Nini

Reputation: 1

python multiple for loop question (pandas, dataframe)

idx_list = []
for idx, row in df_quries_copy.iterrows():
    for brand in brand_name:
        if row['user_query'].contains(brand):
            idx_list.append(idx)
        else:
            continue

brand_name list looks like below

brand_name = ['Apple', 'Lenovo', Samsung', ... ] 

I have df_queries data frame which has the query the user used the table is looks like below

user_query user_id
Apple Laptop A
Lenovo 5GB B

and also I have a brand name as a list i want to find out the users who uses related with brand such as 'Apple laptop'

but when I run the script, I got a message saying that 'str' object has no attribute 'contains'

how am I supposed to do to use multiple for loop ?

Thank you in advance.


for brand in brand_name[:100]:
if len(copy_df[copy_df['user_query'].str.contains(brand)]) >0:
    ls.append(copy_df[copy_df['user_query'].str.contains(brand)].index)
    
else:continue

I tried like answer but the whole dataframe came out in a sudden as a result enter image description here

Upvotes: 0

Views: 73

Answers (1)

Ynjxsjmh
Ynjxsjmh

Reputation: 29982

You can use df_quries_copy[df_quries_copy['user_query'].str.contrains(brand)].index to get index directly.

for brand in brand_name:
    df_quries_copy[df_quries_copy['user_query'].str.contrains(brand)].index

Or in your code, use brand in row['user_query'] since row['user_query'] is a string value.

Upvotes: 1

Related Questions