python_EX

isin : Series의 각 원소가 넘겨받은 연속된 값에 속하는지 나타내는 bool배열을 반환
match : 각 값에 대해 유일한 값을 담고 있는 배열에서의 정수 색인을 계산.
unique : Series에서 중복되는 값을 제거하고 유일한 값만 포함하는 배열을 반환
value_count : Series에서 유일값에 대한 색인과 두수를 계산 (도수는 내림차순)

order_id : 주문번호
quantity : 아이템의 주문수량
item_name : 아이템 이름
choice_description : 주문아이템 상세 선택 옵션
item_price : 주문아이템의 가격

  1. 가장많이 주문한 아이템 top 10
  2. 가장 비싼 아이템 총 몇개 팔렸을까?
  3. Veggie Salad Bowl 이 몇 번 주문되었을까?
In [1]:
import pandas as pd
file_path = 'chipotle.tsv'
chipo = pd.read_csv(file_path,sep='\t')
In [2]:
chipo.head()
Out[2]:
order_id quantity item_name choice_description item_price
0 1 1 Chips and Fresh Tomato Salsa NaN $2.39
1 1 1 Izze [Clementine] $3.39
2 1 1 Nantucket Nectar [Apple] $3.39
3 1 1 Chips and Tomatillo-Green Chili Salsa NaN $2.39
4 2 2 Chicken Bowl [Tomatillo-Red Chili Salsa (Hot), [Black Beans... $16.98

1번

In [3]:
chipo['item_name'].value_counts()[0:10]
Out[3]:
Chicken Bowl                    726
Chicken Burrito                 553
Chips and Guacamole             479
Steak Burrito                   368
Canned Soft Drink               301
Chips                           211
Steak Bowl                      211
Bottled Water                   162
Chicken Soft Tacos              115
Chips and Fresh Tomato Salsa    110
Name: item_name, dtype: int64
In [4]:
chipo['item_price'].value_counts().sort_index()
Out[4]:
$1.09     106
$1.25     264
$1.50     117
$1.69      99
$1.99       1
         ... 
$8.75     730
$8.90      20
$8.99     246
$9.25     398
$9.39      17
Name: item_price, Length: 78, dtype: int64
In [5]:
chipo['item_price'].unique()
Out[5]:
array(['$2.39 ', '$3.39 ', '$16.98 ', '$10.98 ', '$1.69 ', '$11.75 ',
       '$9.25 ', '$4.45 ', '$8.75 ', '$11.25 ', '$8.49 ', '$2.18 ',
       '$8.99 ', '$1.09 ', '$2.95 ', '$2.15 ', '$3.99 ', '$22.50 ',
       '$11.48 ', '$17.98 ', '$17.50 ', '$4.30 ', '$5.90 ', '$1.25 ',
       '$23.78 ', '$6.49 ', '$11.08 ', '$1.50 ', '$22.16 ', '$32.94 ',
       '$22.20 ', '$10.58 ', '$2.50 ', '$23.50 ', '$7.40 ', '$18.50 ',
       '$3.00 ', '$6.78 ', '$11.89 ', '$9.39 ', '$4.00 ', '$3.75 ',
       '$8.69 ', '$2.29 ', '$8.90 ', '$3.27 ', '$3.89 ', '$8.19 ',
       '$35.00 ', '$27.75 ', '$11.80 ', '$6.00 ', '$26.25 ', '$21.96 ',
       '$4.36 ', '$7.50 ', '$4.78 ', '$13.35 ', '$6.45 ', '$5.07 ',
       '$22.96 ', '$7.17 ', '$7.98 ', '$4.50 ', '$26.07 ', '$12.98 ',
       '$35.25 ', '$44.25 ', '$10.50 ', '$33.75 ', '$16.38 ', '$13.52 ',
       '$5.00 ', '$15.00 ', '$8.50 ', '$17.80 ', '$1.99 ', '$11.49 '],
      dtype=object)

3번

In [6]:
sum(chipo[chipo['item_name']=='Veggie Salad Bowl']['quantity'])
Out[6]:
18

2번

  • 가격을 전부 float 로 수정하자
In [7]:
item_name=chipo['item_name']
quantity=chipo['quantity']
item_price=chipo['item_price']
In [8]:
for i in range(len(item_price)):
    item_price[i]=item_price[i].replace('$',"")
    item_price[i]=item_price[i].replace(' ',"")
    item_price[i]=float(item_price[i])

1개의 가격이 가장 높은 것들

In [9]:
item_per_price=item_price/quantity
max(item_per_price)
Out[9]:
11.89
In [10]:
sum(item_per_price==11.89)
Out[10]:
28
In [11]:
item_per_price.sort_values().tail(10)
Out[11]:
1326    11.89
606     11.89
4313    11.89
3208    11.89
749     11.89
4239    11.89
1229    11.89
2439    11.89
2442    11.89
2401    11.89
dtype: object

찾아보면 나오는 메뉴들
Steak Salad Bowl
Barbacoa Salad Bowel
실제로 이것들의 주문을 보면 전부 선택사항이 있었다.

In [12]:
chipo[chipo['item_name']=='Steak Salad Bowl']['choice_description']
Out[12]:
281     [Tomatillo Red Chili Salsa, [Black Beans, Chee...
606     [Fresh Tomato Salsa, [Pinto Beans, Cheese, Gua...
607                         [Fresh Tomato Salsa, Lettuce]
613     [Tomatillo Red Chili Salsa, [Fajita Vegetables...
749     [Roasted Chili Corn Salsa, [Rice, Cheese, Lett...
1159    [Fresh Tomato Salsa, [Rice, Fajita Vegetables,...
1311    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
1505    [Fresh Tomato Salsa, [Rice, Pinto Beans, Chees...
1571    [Fresh Tomato Salsa, [Fajita Vegetables, Sour ...
1590    [Fresh Tomato Salsa, [Fajita Vegetables, Rice,...
1816    [Fresh Tomato Salsa, [Rice, Black Beans, Chees...
2401    [Fresh Tomato Salsa, [Fajita Vegetables, Guaca...
2439    [Fresh Tomato Salsa, [Fajita Vegetables, Rice,...
2600    [Fresh Tomato Salsa, [Fajita Vegetables, Lettu...
2624    [Fresh Tomato Salsa, [Black Beans, Sour Cream,...
2740    [Fresh Tomato Salsa, [Fajita Vegetables, Black...
2804    [Tomatillo Red Chili Salsa, [Rice, Black Beans...
2957    [Fresh Tomato Salsa, [Black Beans, Cheese, Gua...
3098    [Roasted Chili Corn Salsa, [Rice, Black Beans,...
3120    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
3350    [Fresh Tomato Salsa, [Cheese, Guacamole, Lettu...
3493    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4036    [Fresh Tomato Salsa, [Fajita Vegetables, Chees...
4241    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4313    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4391    [Fresh Tomato Salsa, [Black Beans, Pinto Beans...
4419    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4547    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4572    [Fresh Tomato Salsa, [Fajita Vegetables, Lettu...
Name: choice_description, dtype: object
In [13]:
chipo[chipo['choice_description'].isnull()==True][chipo['quantity']==1].sort_values(by='item_price')
Out[13]:
order_id quantity item_name choice_description item_price
1140 471 1 Bottled Water NaN 1.09
3361 1348 1 Bottled Water NaN 1.09
4001 1602 1 Bottled Water NaN 1.09
3499 1405 1 Bottled Water NaN 1.09
2545 1009 1 Bottled Water NaN 1.09
... ... ... ... ... ...
2230 899 1 Chips and Guacamole NaN 4.45
2220 894 1 Chips and Guacamole NaN 4.45
2207 890 1 Chips and Guacamole NaN 4.45
2489 989 1 Chips and Guacamole NaN 4.45
4616 1832 1 Chips and Guacamole NaN 4.45

1164 rows × 5 columns

In [14]:
chipo[chipo['choice_description'].isnull()==True][chipo['quantity']==1]['item_name'].unique()
Out[14]:
array(['Chips and Fresh Tomato Salsa',
       'Chips and Tomatillo-Green Chili Salsa', 'Side of Chips',
       'Chips and Guacamole', 'Bottled Water',
       'Chips and Tomatillo Green Chili Salsa', 'Chips',
       'Chips and Tomatillo Red Chili Salsa',
       'Chips and Roasted Chili-Corn Salsa',
       'Chips and Roasted Chili Corn Salsa',
       'Chips and Tomatillo-Red Chili Salsa',
       'Chips and Mild Fresh Tomato Salsa'], dtype=object)

단품으로 주문한것들에는 포함되지 않는 것들이 많았음

In [15]:
chipo['item_name'].unique()
Out[15]:
array(['Chips and Fresh Tomato Salsa', 'Izze', 'Nantucket Nectar',
       'Chips and Tomatillo-Green Chili Salsa', 'Chicken Bowl',
       'Side of Chips', 'Steak Burrito', 'Steak Soft Tacos',
       'Chips and Guacamole', 'Chicken Crispy Tacos',
       'Chicken Soft Tacos', 'Chicken Burrito', 'Canned Soda',
       'Barbacoa Burrito', 'Carnitas Burrito', 'Carnitas Bowl',
       'Bottled Water', 'Chips and Tomatillo Green Chili Salsa',
       'Barbacoa Bowl', 'Chips', 'Chicken Salad Bowl', 'Steak Bowl',
       'Barbacoa Soft Tacos', 'Veggie Burrito', 'Veggie Bowl',
       'Steak Crispy Tacos', 'Chips and Tomatillo Red Chili Salsa',
       'Barbacoa Crispy Tacos', 'Veggie Salad Bowl',
       'Chips and Roasted Chili-Corn Salsa',
       'Chips and Roasted Chili Corn Salsa', 'Carnitas Soft Tacos',
       'Chicken Salad', 'Canned Soft Drink', 'Steak Salad Bowl',
       '6 Pack Soft Drink', 'Chips and Tomatillo-Red Chili Salsa', 'Bowl',
       'Burrito', 'Crispy Tacos', 'Carnitas Crispy Tacos', 'Steak Salad',
       'Chips and Mild Fresh Tomato Salsa', 'Veggie Soft Tacos',
       'Carnitas Salad Bowl', 'Barbacoa Salad Bowl', 'Salad',
       'Veggie Crispy Tacos', 'Veggie Salad', 'Carnitas Salad'],
      dtype=object)
In [16]:
sum(chipo[chipo['item_name']=='Steak Salad Bowl']['item_price'])/sum(chipo[chipo['item_name']=='Steak Salad Bowl']['quantity'])
Out[16]:
11.083548387096766
In [17]:
mean_price=[]
In [18]:
chipo['item_name'].unique()[0]
Out[18]:
'Chips and Fresh Tomato Salsa'

저희는 절대 추가 사항마다의 가격을 구할 수가 없습니다. 각각의 가격이 적힌 메뉴표가 저희한테는 존재하지 않습니다. 그래서 각 메뉴들의 평균 값을 계산해봅시다. 각 주문에는 그 상품의 가격이 무조건 포함. 선택사항은 사람마다 다릅니다.

In [19]:
for i in chipo['item_name'].unique():
    mean_price.append(sum(chipo[chipo['item_name']==i]['item_price'])/sum(chipo[chipo['item_name']==i]['quantity']))
In [20]:
max(mean_price) 
Out[20]:
11.083548387096766
In [21]:
mean_price
Out[21]:
[2.779692307692303,
 3.3899999999999997,
 3.39,
 2.39,
 9.648791064388927,
 1.6899999999999984,
 9.977797927461117,
 9.57232142857143,
 4.3498814229249145,
 9.442600000000002,
 9.234083333333334,
 9.43455160744498,
 1.090000000000002,
 9.832417582417586,
 9.963833333333334,
 10.376197183098595,
 1.4339336492890997,
 2.950000000000001,
 10.18727272727273,
 2.14930434782608,
 9.989837398373984,
 10.227104072398193,
 10.0184,
 9.636804123711343,
 9.97689655172414,
 9.926111111111112,
 2.9500000000000006,
 10.0175,
 10.13888888888889,
 2.39,
 2.950000000000001,
 9.3985,
 9.01,
 1.25,
 11.083548387096766,
 6.490000000000004,
 2.39,
 7.4,
 7.3999999999999995,
 7.4,
 9.745000000000001,
 8.915,
 3.0,
 9.245000000000001,
 11.056666666666667,
 10.64,
 7.4,
 8.49,
 8.49,
 8.99]

그렇게 찾은 메뉴

In [22]:
chipo['item_name'].unique()[-16]
Out[22]:
'Steak Salad Bowl'
In [23]:
sum(chipo[chipo['item_name']=='Steak Salad Bowl']['quantity'])
Out[23]:
31
In [24]:
chipo[chipo['item_name']=='Steak Salad Bowl']['choice_description']
Out[24]:
281     [Tomatillo Red Chili Salsa, [Black Beans, Chee...
606     [Fresh Tomato Salsa, [Pinto Beans, Cheese, Gua...
607                         [Fresh Tomato Salsa, Lettuce]
613     [Tomatillo Red Chili Salsa, [Fajita Vegetables...
749     [Roasted Chili Corn Salsa, [Rice, Cheese, Lett...
1159    [Fresh Tomato Salsa, [Rice, Fajita Vegetables,...
1311    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
1505    [Fresh Tomato Salsa, [Rice, Pinto Beans, Chees...
1571    [Fresh Tomato Salsa, [Fajita Vegetables, Sour ...
1590    [Fresh Tomato Salsa, [Fajita Vegetables, Rice,...
1816    [Fresh Tomato Salsa, [Rice, Black Beans, Chees...
2401    [Fresh Tomato Salsa, [Fajita Vegetables, Guaca...
2439    [Fresh Tomato Salsa, [Fajita Vegetables, Rice,...
2600    [Fresh Tomato Salsa, [Fajita Vegetables, Lettu...
2624    [Fresh Tomato Salsa, [Black Beans, Sour Cream,...
2740    [Fresh Tomato Salsa, [Fajita Vegetables, Black...
2804    [Tomatillo Red Chili Salsa, [Rice, Black Beans...
2957    [Fresh Tomato Salsa, [Black Beans, Cheese, Gua...
3098    [Roasted Chili Corn Salsa, [Rice, Black Beans,...
3120    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
3350    [Fresh Tomato Salsa, [Cheese, Guacamole, Lettu...
3493    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4036    [Fresh Tomato Salsa, [Fajita Vegetables, Chees...
4241    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4313    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4391    [Fresh Tomato Salsa, [Black Beans, Pinto Beans...
4419    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4547    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4572    [Fresh Tomato Salsa, [Fajita Vegetables, Lettu...
Name: choice_description, dtype: object
In [25]:
#비슷한 가격의 다른 제품을 보자
chipo['item_name'].unique()[-6]
Out[25]:
'Carnitas Salad Bowl'

추가사항이 비슷함

In [26]:
chipo[chipo['item_name']=='Carnitas Salad Bowl']['choice_description']
Out[26]:
1132    [Fresh Tomato Salsa, [Rice, Black Beans, Chees...
1865     [Fresh Tomato Salsa, [Rice, Cheese, Sour Cream]]
2610    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
3115    [Tomatillo Green Chili Salsa, [Rice, Pinto Bea...
3749    [Roasted Chili Corn Salsa, [Fajita Vegetables,...
4239    [Tomatillo Green Chili Salsa, [Black Beans, Ch...
Name: choice_description, dtype: object
In [27]:
sum(chipo[chipo['item_name']=='Steak Salad Bowl']['quantity'])
Out[27]:
31

+ Recent posts