Pandas.DataFrame.Apply-TypeError:类型'float'的参数不可迭代

时间:2018-09-15 15:49:30

标签: python pandas dataframe iteration

我试图仅提取其中带有“ Restaurants”一词的类别,所以我使用了lambda函数来对意甲进行迭代。

# only keep necessary columns
keeps = ['name', 'attributes', 'stars', 'categories', 'review_count', 'business_id']
business_df = business_df[keeps]

# clean json
business_df.categories = business_df.categories[business_df.categories.notnull()].apply(lambda categories: set(categories))

# keep only restaurants
restaurant_df = business_df[business_df.categories.apply(
                lambda categories: 'Restaurants' in categories)].copy()

但是我得到了这个堆栈跟踪:

Traceback (most recent call last):
File "C:/Users/leona/Desktop/progettoML/opinion-mining-master/modeling/0_data_prep.py", line 56, in <module>
lambda categories: 'Restaurants' in categories)].copy()
File "C:\Users\leona\Desktop\progettoML\venv\lib\site-packages\pandas\core\series.py", line 3194, in apply
mapped = lib.map_infer(values, f, convert=convert_dtype)
File "pandas/_libs/src\inference.pyx", line 1472, in pandas._libs.lib.map_infer
File "C:/Users/leona/Desktop/progettoML/opinion-mining-master/modeling/0_data_prep.py", line 56, in <lambda>
lambda categories: 'Restaurants' in categories)].copy()
TypeError: argument of type 'float' is not iterable

如您所见,我在Windows上,并且我正在使用python 2.7.15。 我看到了与此错误相关的不同问题,但我仍然没有弄清楚为什么它不起作用。

这是json文件中的几行:

{"business_id":"45bWSZtniwPRiqlivpS8Og","name":"The Coffee Bean & Tea Leaf","neighborhood":"","address":"20235 N Cave Creek Rd, Ste 1115","city":"Phoenix","state":"AZ","postal_code":"85024","latitude":33.6713751,"longitude":-112.0300171,"stars":4.0,"review_count":63,"is_open":1,"attributes":{"BikeParking":"True","BusinessAcceptsCreditCards":"True","BusinessParking":"{'garage': False, 'street': False, 'validated': False, 'lot': True, 'valet': False}","Caters":"False","OutdoorSeating":"True","RestaurantsPriceRange2":"1","RestaurantsTakeOut":"True","WheelchairAccessible":"True","WiFi":"free"},"categories":"Coffee & Tea, Food","hours":{"Monday":"5:30-20:0","Tuesday":"5:30-20:0","Wednesday":"5:30-20:0","Thursday":"5:30-20:0","Friday":"5:30-21:0","Saturday":"5:30-21:0","Sunday":"6:30-19:0"}}
{"business_id":"9A2quhZLyWk0akUetBd8hQ","name":"Bnc Cake House","neighborhood":"Koreatown","address":"631 Bloor St W","city":"Toronto","state":"ON","postal_code":"M6G 1K8","latitude":43.6643776,"longitude":-79.4144238,"stars":4.0,"review_count":7,"is_open":0,"attributes":{"BusinessParking":"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}","RestaurantsPriceRange2":"1"},"categories":"Food, Bakeries","hours":null}
{"business_id":"6OuOZAok8ikONMS_T3EzXg","name":"Thai One On","neighborhood":"Ridgewood","address":"3417 Derry Road E, Unit 103","city":"Mississauga","state":"ON","postal_code":"L4T 1A8","latitude":43.7129464,"longitude":-79.6327631,"stars":2.0,"review_count":7,"is_open":1,"attributes":{"Alcohol":"none","BusinessAcceptsCreditCards":"True","BusinessParking":"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}","GoodForKids":"True","OutdoorSeating":"False","RestaurantsAttire":"casual","RestaurantsGoodForGroups":"True","RestaurantsPriceRange2":"2","RestaurantsTableService":"False","RestaurantsTakeOut":"True"},"categories":"Restaurants, Thai","hours":null}

输出print(business_df.head(10).to_dict()):

{u'review_count': {0: 24L, 1: 3L, 2: 5L, 3: 8L, 4: 4L, 5: 63L, 6: 7L, 7: 7L, 8: 40L, 9: 3L}, 
u'name': {0: u'Minhas Micro Brewery', 1: u"CK'S BBQ & Catering", 2: u'La Bastringue', 3: u'Geico Insurance', 4: u'Action Engine', 5: u'The Coffee Bean & Tea Leaf', 6: u'Bnc Cake House', 7: u'Thai One On', 8: u"Filiberto's Mexican Food", 9: u"Maggie & Stella's Gifts"},
u'business_id': {0: u'Apn5Q_b6Nz61Tq4XzPdf9A', 1: u'AjEbIBw6ZFfln7ePHha9PA', 2: u'O8S5hYJ1SMc8fA4QBtVujA', 3: u'bFzdJJ3wp3PZssNEsyU23g', 4: u'8USyCYqpScwiNEb58Bt6CA', 5: u'45bWSZtniwPRiqlivpS8Og', 6: u'9A2quhZLyWk0akUetBd8hQ', 7: u'6OuOZAok8ikONMS_T3EzXg', 8: u'8-NRKkPY1UiFXW20WXKiXg', 9: u'UTm5QZThPQlT35mkAcGOjg'},
u'stars': {0: 4.0, 1: 4.5, 2: 4.0, 3: 1.5, 4: 2.0, 5: 4.0, 6: 4.0, 7: 2.0, 8: 2.5, 9: 3.5},
u'attributes': {0: {u'BusinessParking': u"{'garage': False, 'street': True, 'validated': False, 'lot': False, 'valet': False}", u'HasTV': u'True', u'RestaurantsGoodForGroups': u'True', u'NoiseLevel': u'average', u'RestaurantsAttire': u'casual', u'RestaurantsReservations': u'True', u'OutdoorSeating': u'False', u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'2', u'BikeParking': u'False', u'RestaurantsDelivery': u'False', u'RestaurantsTakeOut': u'True', u'GoodForKids': u'True'}, 1: {u'RestaurantsTableService': u'False', u'GoodForMeal': u"{'dessert': False, 'latenight': False, 'lunch': False, 'dinner': False, 'breakfast': False, 'brunch': False}", u'DogsAllowed': u'True', u'Alcohol': u'none', u'Caters': u'True', u'HasTV': u'False', u'RestaurantsGoodForGroups': u'True', u'WiFi': u'no', u'RestaurantsAttire': u'casual', u'RestaurantsReservations': u'False', u'OutdoorSeating': u'True', u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'2', u'WheelchairAccessible': u'True', u'BikeParking': u'False', u'RestaurantsDelivery': u'False', u'RestaurantsTakeOut': u'True', u'GoodForKids': u'True', u'DriveThru': u'False', u'BusinessParking': u"{'garage': False, 'street': True, 'validated': False, 'lot': True, 'valet': False}"}, 2: {u'RestaurantsTableService': u'True', u'GoodForMeal': u"{'dessert': False, 'latenight': False, 'lunch': False, 'dinner': False, 'breakfast': False, 'brunch': False}", u'Alcohol': u'beer_and_wine', u'Caters': u'False', u'HasTV': u'True', u'RestaurantsGoodForGroups': u'True', u'NoiseLevel': u'average', u'WiFi': u'free', u'RestaurantsAttire': u'casual', u'RestaurantsReservations': u'True', u'OutdoorSeating': u'False', u'BusinessAcceptsCreditCards': u'False', u'RestaurantsPriceRange2': u'2', u'BikeParking': u'True', u'RestaurantsDelivery': u'False', u'Ambience': u"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': False}", u'RestaurantsTakeOut': u'False', u'GoodForKids': u'True', u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}"}, 3: None, 4: {u'BusinessAcceptsCreditCards': u'True'}, 5: {u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': True, 'valet': False}", u'Caters': u'False', u'WiFi': u'free', u'OutdoorSeating': u'True', u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'1', u'BikeParking': u'True', u'RestaurantsTakeOut': u'True', u'WheelchairAccessible': u'True'}, 6: {u'RestaurantsPriceRange2': u'1', u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}"}, 7: {u'Alcohol': u'none', u'RestaurantsGoodForGroups': u'True', u'RestaurantsAttire': u'casual', u'OutdoorSeating': u'False', u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'2', u'RestaurantsTableService': u'False', u'RestaurantsTakeOut': u'True', u'GoodForKids': u'True', u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}"}, 8: {u'RestaurantsTableService': u'False', u'GoodForMeal': u"{'dessert': False, 'latenight': True, 'lunch': False, 'dinner': False, 'breakfast': False, 'brunch': False}", u'Alcohol': u'none', u'Caters': u'False', u'HasTV': u'False', u'RestaurantsGoodForGroups': u'True', u'NoiseLevel': u'average', u'WiFi': u'no', u'RestaurantsAttire': u'casual', u'RestaurantsReservations': u'False', u'OutdoorSeating': u'False', u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'1', u'BikeParking': u'True', u'RestaurantsDelivery': u'False', u'Ambience': u"{'romantic': False, 'intimate': False, 'classy': False, 'hipster': False, 'divey': False, 'touristy': False, 'trendy': False, 'upscale': False, 'casual': True}", u'RestaurantsTakeOut': u'True', u'GoodForKids': u'True', u'WheelchairAccessible': u'True', u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': True, 'valet': False}"}, 9: {u'BusinessAcceptsCreditCards': u'True', u'RestaurantsPriceRange2': u'2', u'BusinessParking': u"{'garage': False, 'street': False, 'validated': False, 'lot': False, 'valet': False}", u'BikeParking': u'True'}},
u'categories': {0: u'Tours, Breweries, Pizza, Restaurants, Food, Hotels & Travel', 1: u'Chicken Wings, Burgers, Caterers, Street Vendors, Barbeque, Food Trucks, Food, Restaurants, Event Planning & Services', 2: u'Breakfast & Brunch, Restaurants, French, Sandwiches, Cafes', 3: u'Insurance, Financial Services', 4: u'Home & Garden, Nurseries & Gardening, Shopping, Local Services, Automotive, Electronics Repair', 5: u'Coffee & Tea, Food', 6: u'Food, Bakeries', 7: u'Restaurants, Thai', 8: u'Mexican, Restaurants', 9: u'Flowers & Gifts, Gift Shops, Shopping'}}

1 个答案:

答案 0 :(得分:0)

我想出了简单地使用str(categories)解析类别的方法。这比我想的要容易。谢谢大家。