0

I have a df (for example purposes, I created a small sample). One column is whatever, second column is a type of Cuisine.

df = pd.DataFrame({'Name': ['a', 'b', 'c', 'd'],
              'Cuisine': ['Mexican', 'Asian', 'Italian', 'Mexican']})

    Name    Cuisine
0   a       Mexican
1   b       Asian
2   c       Italian
3   d       Mexican

Now, I want to remove all rows that fall into a specific cuisine. For this, I created a list of cuisines to remove and used the following code, iterating over rows in the df. I have read this post and I realize this is ineffective (it took 2 minutes just for a 7k row dataset), but I can't figure a better way on my own.

Desired outcome:

    Name    Cuisine
0   c       Italian

This is the code I used:

cuisines_to_drop = ['Mexican', 'Asian'] #create list of unwanted cuisines

new_rows = [] #create list of new rows

df_new = pd.DataFrame() #create new df

for index, row in df.iterrows():
    if row['Cuisine'] in cuisines_to_drop:
        continue
    else:
        new_row = row #add new row
        new_rows.append(new_row.values) #append new row
    df_new = dfNewRows.append(pd.DataFrame(new_rows, columns = df.columns)) #add all new rows to df

There must be a more efficient and "pythonic" way to do this.

JachymDvorak
  • 129
  • 8

0 Answers0