题目数据来源:https://www.kesci.com/mw/notebook/5c1b599d8859e0002b718378
了解你的数据 -- 探索Chipotle快餐数据
1. 导入需要的库
import pandas as pd
2. 导入数据
chipo = pd.read_csv('chipotle.tsv', sep='\t')
3. 查看前10行内容
chipo.head(10)
4. 数据集有多少列(columns)
# print(chipo.shape) (4622, 5)
# type(chipo.shape) tuple 元祖类型 元祖是根据索引来获取元素的
chipo.shape[1]
5. 打印出全部列的名称
chipo.columns
6. 数据集的索引是怎样的
chipo.index
7. 被下单最多的商品是什么
# 分组求和 排序
chipo.groupby(by='item_name').quantity.sum().sort_values(ascending=False).reset_index().head(1)
8.在item_name这一列,一共有多少种商品被下单
chipo.item_name.nunique()
9. 在choice_description中,下单次数最多的商品是什么
chipo.groupby('choice_description').quantity.count().sort_values(ascending=False).reset_index().head()
# 或:
# value_counts 结果默认排序
chipo.choice_description.value_counts().reset_index().head()
具体参数可查看博客:https://blog.csdn.net/dzjun/article/details/84925056
10. 一共有多少商品被下单
chipo.quantity.sum()
11. 将item_price转换为浮点数
# 先转换为str,再替换掉美元($)符号,最后再转换为浮点类型(float)
chipo['item_price'] = chipo.item_price.str.replace('$', '').astype(float)
12. 在数据集对应的时期内,收入(revenue)是多少
chipo['sub_total'] = round(chipo.quantity * chipo.item_price, 2)
chipo.sub_total.sum()
13. 在数据集对应的时期内,一共有多少订单
chipo.order_id.nunique()
14. 每一单对应的平均总价是多少
chipo.groupby('order_id').sub_total.sum().mean()
15. 一共有多少种不同的商品被售出
chipo.item_name.nunique()