pythonbook/实验 探索Chipotle快餐数据/1.探索Chipotle快餐数据.py

60 lines
2.2 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -- 将数据集存入一个名为chipo的数据框内
# -- 查看前10行内容
# -- 数据集中有多少个列(columns)
# -- 打印出全部的列名称
# -- 数据集的索引是怎样的?
# -- 被下单数最多商品(item)是什么?
# -- 在item_name这一列中一共有多少种商品被下单
# -- 在choice_description中下单次数最多的商品是什么
# -- 一共有多少商品被下单?
# -- 将item_price转换为浮点数
# -- 在该数据集对应的时期内,收入(revenue)是多少?
# -- 在该数据集对应的时期内,一共有多少订单?
# -- 每一单(order)对应的平均总价是多少?
import pandas as pd
#将数据集存入一个名为chipo的数据框内
chipo = pd.read_csv('./data/chipotle.tsv',sep='\t')
print(chipo.head(10))
#数据集中有多少个列(columns)
print(chipo.shape)
print(chipo.shape[1])
print(chipo.columns)
print(chipo.index)
#被下单数最多商品(item)是什么?
# print(chipo[['item_name','quantity']].groupby(by=['item_name']).describe())
a1 = chipo[['item_name','quantity']].groupby(by=['item_name']).sum().sort_values(by = ['quantity'],ascending=False)
print(a1)
#在item_name这一列中一共有多少种商品被下单
a2 = chipo['item_name'].nunique()
print(a2)
#在choice_description中下单次数最多的商品是什么
a3 = chipo[['quantity','choice_description']].groupby('choice_description').sum().sort_values('quantity',ascending=False)
a4 = chipo['choice_description'].value_counts().head()
print(a3,a4)
#一共有多少商品被下单?
a5 = chipo['quantity'].sum()
print(a5)
#将item_price转换为浮点数
#货币符号后取起
# print(chipo['item_price'])
#为什么从0开始不行
#报错could not convert string to float: '$2.39 '
a6 = chipo['item_price'] = chipo['item_price'].apply(lambda x:float(x[1:]))
#在该数据集对应的时期内,收入(revenue)是多少?
((chipo['quantity']*chipo['item_price'])).sum()
#在该数据集对应的时期内,一共有多少订单?
chipo['order_id'].nunique()
#每一单(order)对应的平均总价是多少?
chipo['item_price_sum'] = chipo['quantity'] * chipo['item_price']
chipo[['order_id','item_price_sum']].groupby('order_id').sum().mean()