數(shù)據(jù)源:鏈接: https://pan.baidu.com/s/1EFqJFXf70t2Rubkh6D19aw 提取碼: syqg
數(shù)據(jù)源示例:
步驟1 導(dǎo)入必要的庫(kù)
import pandas as pd
步驟2 從如下地址導(dǎo)入數(shù)據(jù)集
path1='pandas_exercise\exercise_data\chipotle.tsv'
步驟3 將數(shù)據(jù)集存入一個(gè)名為chipo的數(shù)據(jù)框內(nèi)
chipo=pd.read_csv(path1,sep='\t')
步驟4 查看前10行內(nèi)容
print(chipo.head())
步驟6 數(shù)據(jù)集中有多少個(gè)列(columns)
print(chipo.shape[1])
步驟7 打印出全部的列名稱
print(chipo.columns)
步驟8 數(shù)據(jù)集的索引是怎樣的
print(chipo.index)
步驟9 被下單數(shù)最多商品(item)是什么?
c=chipo[['item_name','quantity']].groupby(['item_name']).agg({'quantity':'sum'})
c.sort_values(['quantity'],ascending=False,inplace=True)
print(c.head())
步驟10 在item_name這一列中,一共有多少種商品被下單? nunique()去重計(jì)數(shù)
print(chipo['item_name'].nunique())
步驟11 在choice_description中,下單次數(shù)最多的商品是什么?
b=chipo['choice_description'].value_counts().head()
print(b)
步驟12 一共有多少商品被下單?
print(chipo['quantity'].sum())
步驟13 將item_price轉(zhuǎn)換為浮點(diǎn)數(shù)
print(chipo['item_price'].head())
fd=lambda x:float(x[1:-1])
chipo['item_price']=chipo['item_price'].apply(fd)
print(chipo['item_price'].head())
步驟14 在該數(shù)據(jù)集對(duì)應(yīng)的時(shí)期內(nèi),收入(revenue)是多少
chipo['total_sum']=round(chipo['quantity']*chipo['item_price'],2)
a=chipo['total_sum'].sum()
print(a)
步驟15 在該數(shù)據(jù)集對(duì)應(yīng)的時(shí)期內(nèi),一共有多少訂單?
e=chipo['order_id'].nunique()
print(e)
步驟16 每一單(order)對(duì)應(yīng)的平均總價(jià)是多少?
print(a/e)
or
f=chipo[['total_sum','order_id']].groupby('order_id').agg({'total_sum':'sum'})['total_sum'].mean()
print(f)
步驟17 一共有多少種不同的商品被售出?
print(chipo['item_name'].nunique())
輸出
#步驟4
order_id ... item_price
0 1 ... $2.39
1 1 ... $3.39
2 1 ... $3.39
3 1 ... $2.39
4 2 ... $16.98
[5 rows x 5 columns]
#步驟6
5
#步驟7
Index(['order_id', 'quantity', 'item_name', 'choice_description',
'item_price'],
dtype='object')
#步驟8
RangeIndex(start=0, stop=4622, step=1)
#步驟9
quantity
item_name
Chicken Bowl 761
Chicken Burrito 591
Chips and Guacamole 506
Steak Burrito 386
Canned Soft Drink 351
#步驟10
50
#步驟11
[Diet Coke] 134
[Coke] 123
[Sprite] 77
[Fresh Tomato Salsa, [Rice, Black Beans, Cheese, Sour Cream, Lettuce]] 42
[Fresh Tomato Salsa, [Rice, Black Beans, Cheese, Sour Cream, Guacamole, Lettuce]] 40
Name: choice_description, dtype: int64
#步驟12
4972
#步驟13
0 $2.39
1 $3.39
2 $3.39
3 $2.39
4 $16.98
Name: item_price, dtype: object
0 2.39
1 3.39
2 3.39
3 2.39
4 16.98
Name: item_price, dtype: float64
#步驟14
39237.02
#步驟15
1834
#步驟16
21.39423118865867
#步驟17
50