import pandas as pd
businesses = pd.read_json(businesses_filepath, lines=True, encoding='utf_8')
restaurantes = businesses['Restaurants' in businesses['categories']]
我想刪除沒有在類別列餐廳的線條,這列有清單,但給了錯誤「KeyError異常:假」,我想明白爲什麼以及如何解決。KeyError異常:假的熊貓數據幀
import pandas as pd
businesses = pd.read_json(businesses_filepath, lines=True, encoding='utf_8')
restaurantes = businesses['Restaurants' in businesses['categories']]
我想刪除沒有在類別列餐廳的線條,這列有清單,但給了錯誤「KeyError異常:假」,我想明白爲什麼以及如何解決。KeyError異常:假的熊貓數據幀
表達式'Restaurants' in businesses['categories']
返回布爾值False
。這被傳遞給DataFrame業務的括號索引操作符,它不包含名爲False的列,因此引發KeyError。
你在做什麼是所謂的布爾索引,它是這樣工作的。
businesses[businesses['categories'] == 'Restaurants']
同樣的想法,不同的語法,upvote,做得好 –
我想你的意思是:
businesses = businesses.loc[businesses['categories'] == 'Restaurants']
,將只保留行與該類別的餐館
如果您發現您的數據包含拼寫變化或替代餐廳相關方面,以下可能會有好處。基本上你把你的餐廳相關的條款在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))]
企業中的'餐廳'['categories']'是一個本地python表達式,它的計算結果爲標量'False'(或'True')。 「餐廳」不只是其中的一個類別?在這種情況下,你需要'企業[business.categories =='餐廳']'。 –