KeyError: False in pandas dataframe

Viewed 44985
import pandas as pd

businesses = pd.read_json(businesses_filepath, lines=True, encoding='utf_8')
restaurantes = businesses['Restaurants' in businesses['categories']]

I would like to remove the lines that do not have Restaurants in the categories column, and this column has lists, however gave the error 'KeyError: False' and I would like to understand why and how to solve.

5 Answers

If you find that your data contains spelling variations or alternative restaurant related terms, the following may be of benefit. Essentially you put your restaurant related terms in restuarant_lst. The lambda function returns true if any of the items in restaurant_lst are contained within each row of the business series. The .loc indexer filters out rows which return false for the lambda function.

restaurant_lst = ['Restaurant','restaurantes','diner','bistro']
restaurant = businesses.loc[businesses.apply(lambda x: any(restaurant_str in x for restaurant_str in restaurant_lst))]

The reason for this is that the Series class implements a custom in operator that doesn't return an iterable like the == does, here's a workaround

businesses[['Restaurants' in c for c in list(businesses['categories'])]]

hopefully this helps someone where you're looking for a substring in the column and not a full match.

None of the answers here actually worked for me,

businesses[businesses['categories'] == 'Restaurants']

obviously won't work since the value in 'categories' is not a string, it's a list, meaning the comparison will always fail.

What does, however, work, is converting the column into tuples instead of strings:

businesses['categories'] = businesses['categories'].apply(tuple)

That allows you to use the standard .loc thing:

businesses.loc[businesses['categories'] == ('Restaurants',)]
Related