KeyError:Pandas数据框中的False [英] KeyError: False in pandas dataframe
问题描述
import pandas as pd
businesses = pd.read_json(businesses_filepath, lines=True, encoding='utf_8')
restaurantes = businesses['Restaurants' in businesses['categories']]
我想删除类别列中没有餐厅的行,并且此列具有列表,但是给出了错误"KeyError:False",我想了解为什么以及如何解决.
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.
推荐答案
表达式'Restaurants' in businesses['categories']
返回布尔值False
.这将传递给DataFrame业务的方括号索引运算符,该运算符不包含名为False的列,因此会引发KeyError.
The expression 'Restaurants' in businesses['categories']
returns the boolean value False
. This is passed to the brackets indexing operator for the DataFrame businesses which does not contain a column called False and thus raises a KeyError.
您要执行的操作就是布尔索引,它的工作原理如下.
What you are looking to do is something called boolean indexing which works like this.
businesses[businesses['categories'] == 'Restaurants']
这篇关于KeyError:Pandas数据框中的False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!