2017-07-02 111 views
1
import pandas as pd 

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

我想刪除沒有在類別列餐廳的線條,這列有清單,但給了錯誤「KeyError異常:假」,我想明白爲什麼以及如何解決。KeyError異常:假的熊貓數據幀

+2

企業中的'餐廳'['categories']'是一個本地python表達式,它的計算結果爲標量'False'(或'True')。 「餐廳」不只是其中的一個類別?在這種情況下,你需要'企業[business.categories =='餐廳']'。 –

回答

3

表達式'Restaurants' in businesses['categories']返回布爾值False。這被傳遞給DataFrame業務的括號索引操作符,它不包含名爲False的列,因此引發KeyError。

你在做什麼是所謂的布爾索引,它是這樣工作的。

businesses[businesses['categories'] == 'Restaurants'] 
+0

同樣的想法,不同的語法,upvote,做得好 –

1

我想你的意思是:

businesses = businesses.loc[businesses['categories'] == 'Restaurants'] 

,將只保留行與該類別的餐館

0

如果您發現您的數據包含拼寫變化或替代餐廳相關方面,以下可能會有好處。基本上你把你的餐廳相關的條款在restuarant_lst。如果restaurant_lst中的任何項目包含在業務系列的每一行中,則lambda函數將返回true.loc索引器篩選出行,返回false用於lambda函數。

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