程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Actual case of data analysis: the use of pandas in restaurant scoring data

編輯:Python

brief introduction

In order to better master pandas Application in actual data analysis , Today, let's talk about how to use pandas Analyze the data of American restaurants .

Introduction to restaurant scoring data

The source of the data is UCI ML Repository, Contains more than 1000 pieces of data , Yes 5 Attributes , Namely :

userID: user ID

placeID: The restaurant ID

rating: Overall rating

food_rating: Food score

service_rating: Service rating

We use pandas To read data :

import numpy as np
path = '../data/restaurant_rating_final.csv'
df = pd.read_csv(path)
df
userIDplaceIDratingfood_ratingservice_rating0U10771350852221U10771350382212U10771328252223U10771350601224U1068135104112………………1156U10431326301111157U10111327151101158U10681327331101159U10681325941111160U1068132660000

1161 rows × 5 columns

Analyze scoring data

If we focus on the total score and food score of different restaurants , Let's look at the average score of these restaurants first , Here we use pivot_table Method :

mean_ratings = df.pivot_table(values=['rating','food_rating'], index='placeID',
aggfunc='mean')
mean_ratings[:5]
food_ratingratingplaceID1325601.000.501325611.000.751325641.251.251325721.001.001325831.001.00

Then look at each placeID, Statistics on the number of voters :

ratings_by_place = df.groupby('placeID').size()
ratings_by_place[:10]
placeID
132560 4
132561 4
132564 4
132572 15
132583 4
132584 6
132594 5
132608 6
132609 5
132613 6
dtype: int64

If the turnout is too small , So these data are actually not objective , Let's pick a number of people who voted more than 4 A restaurant :

active_place = ratings_by_place.index[ratings_by_place >= 4]
active_place
Int64Index([132560, 132561, 132564, 132572, 132583, 132584, 132594, 132608,
132609, 132613,
...
135080, 135081, 135082, 135085, 135086, 135088, 135104, 135106,
135108, 135109],
dtype='int64', name='placeID', length=124)

Choose the average rating data of these restaurants :

mean_ratings = mean_ratings.loc[active_place]
mean_ratings
food_ratingratingplaceID1325601.0000000.5000001325611.0000000.7500001325641.2500001.2500001325721.0000001.0000001325831.0000001.000000………1350881.1666671.0000001351041.4285710.8571431351061.2000001.2000001351081.1818181.1818181351091.2500001.000000

124 rows × 2 columns

Yes rating Sort , Choose the one with the highest score 10 individual :

top_ratings = mean_ratings.sort_values(by='rating', ascending=False)
top_ratings[:10]
food_ratingratingplaceID1329551.8000002.0000001350342.0000002.0000001349862.0000002.0000001329221.5000001.8333331327552.0000001.8000001350741.7500001.7500001350132.0000001.7500001349761.7500001.7500001350551.7142861.7142861350751.6923081.692308

We can also calculate the difference between the average total score and the average food score , And with a column diff Preservation :

mean_ratings['diff'] = mean_ratings['rating'] - mean_ratings['food_rating']
sorted_by_diff = mean_ratings.sort_values(by='diff')
sorted_by_diff[:10]
food_ratingratingdiffplaceID1326672.0000001.250000-0.7500001325941.2000000.600000-0.6000001328581.4000000.800000-0.6000001351041.4285710.857143-0.5714291325601.0000000.500000-0.5000001350271.3750000.875000-0.5000001327401.2500000.750000-0.5000001349921.5000001.000000-0.5000001327061.2500000.750000-0.5000001328701.0000000.600000-0.400000

Invert the data , Choose the front with the largest gap 10:

sorted_by_diff[::-1][:10]
food_ratingratingdiffplaceID1349870.5000001.0000000.5000001329371.0000001.5000000.5000001350661.0000001.5000000.5000001328511.0000001.4285710.4285711350490.6000001.0000000.4000001329221.5000001.8333330.3333331350301.3333331.5833330.2500001350631.0000001.2500000.2500001326261.0000001.2500000.2500001350001.0000001.2500000.250000

Calculation rating Standard deviation , And choose the largest front 10 individual :

# Standard deviation of rating grouped by placeID
rating_std_by_place = df.groupby('placeID')['rating'].std()
# Filter down to active_titles
rating_std_by_place = rating_std_by_place.loc[active_place]
# Order Series by value in descending order
rating_std_by_place.sort_values(ascending=False)[:10]
placeID
134987 1.154701
135049 1.000000
134983 1.000000
135053 0.991031
135027 0.991031
132847 0.983192
132767 0.983192
132884 0.983192
135082 0.971825
132706 0.957427
Name: rating, dtype: float64

This article has been included in http://www.flydean.com/02-pandas-restaurant/

The most popular interpretation , The deepest dry goods , The most concise tutorial , There are so many tricks you don't know about waiting for you to discover !

Welcome to my official account. :「 Program those things 」, Know technology , Know you better !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved