1

I have a dataframe that contains a column with different submarkets from a city. I need to iterate through that column and check if a value in that row matches any of the entries that could be in the list. that list will then be added as a column to the original dataframe

submarket_list = [ 'South Financial District', 'Financial District', South of Market]

submarket_check = []

for index,row in test_df.iterrows():
    for j in submarket_list:
        if row['Submarket'] == j:
            submarket_check.append("yes")
        else:
            submarket_check.append("No")
test_df['Submarket Check'] = submarket_check
test_df
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • 1
    `test_df['Submarket Check'] = np.where(test_df["Submarket"].isin(submarket_list), 'yes', 'No')` Examples [here](https://stackoverflow.com/a/45921117/15497888) and [here](https://stackoverflow.com/a/19913845/15497888) (`import numpy as np` ) – Henry Ecker Oct 21 '21 at 23:56

1 Answers1

3
test_df["Submarket"].isin(submarket_list)

will give you a column of booleans. It's all you need

Riley
  • 2,153
  • 1
  • 6
  • 16