0

I have two spreadsheets that both contain UK postcodes, which I'm loading into two pandas DataFrames. I need to go through each row in one DataFrame and check if the 'postcode' value is contained in the 'postcode' column of the second DataFrame.

The code I expected to work is:

data_to_process = pd.read_excel('input.xlsx')
postcodes = pd.read_excel('salespeople_patches.xlsx')

salesperson_postcodes_list = postcodes[postcodes['Salesperson']=='Dave']['Postcode'].to_list()

data = data_to_process[data_to_process['District'] in salesperson_postcodes_list]

The error I'm getting is:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Ch3steR
  • 20,090
  • 4
  • 28
  • 58
Anders
  • 115
  • 11

1 Answers1

0

Use pd.Series.isin

df = pd.DataFrame({'col1':[*'ABCDEFG']})
df['Found'] = df['col1'].isin([*'ACF'])

Output:

  col1  Found
0    A   True
1    B  False
2    C   True
3    D  False
4    E  False
5    F   True
6    G  False
Scott Boston
  • 147,308
  • 15
  • 139
  • 187