本次实验内容为餐饮订单数据的分析,数据请见:https://pan.baidu.com/s/1tL7FE5lxs-gb6Phf8XRu_Q,文件夹:data_analysis,下面的文件:chipotle.csv 本次实验主要是对python中的数据进行基本操作。
代码为:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
#用 shift+Alt+E一行一行执行,右下角看数据的 Special Variables下面的chipo执行情况
import pandas as pd
# 1. 将数据集存入一个名为 chipo的数据框内
chipo = pd.read_csv('data_analysis/chipotle.csv')
# 2. 查看前10行内容
chipo.head(10)
# 3. 数据集中有多少个列(columns) ?
chipo.shape[1]
# 4. 打印出全部的列名称
chipo.columns
# 5. 数据集的索引是怎样?
chipo.index
# 6. 被下单数最多商品 (item)是什么?涉及分组聚合,idxmax()表示最大值的索引 改成max()则可以看到最大值是多少
chipo.groupby('item_name').agg({'quantity': 'sum'}).idxmax()
chipo.groupby('item_name').agg({'quantity': 'sum'}).max()
chipo.groupby('item_name').agg({'quantity': 'sum'}).idxmin()
chipo.groupby('item_name').agg({'quantity': 'sum'}).min()
# 7. 在 item_name这一列中,共有多少种商品被下单?unique()是去重操作 nunique()是去重之后的个数
chipo['item_name'].unique()
chipo['item_name'].nunique()
# 8. 一共有多少个商品被下单?
chipo['quantity'].sum()
# 9. 将item_price转换为浮点数
# chipo['item_price'][0][1:]
chipo['item_price'] = chipo['item_price'].str[1:].astype('float') #取字符串的从1位置开始的数据
# 10. 在该数据集对应的时期内,收入 (revenue) 是多少?
chipo['revenue'] = chipo['quantity']*chipo['item_price']
chipo['revenue'].sum()
# 11. 在该数据集对应的时期内,一共有多少订单?
chipo['order_id'].nunique()
# 12. 每一单 (order) 对应的平均总价是多少?
chipo['revenue'].sum()/chipo['order_id'].nunique()
文件‘chipotle.csv’中的数据截图为:
运行结果截图如下所示: