kesci-这十套练习,教你如何使用Pandas做数据分析-练习1(学习笔记)

题目数据来源:https://www.kesci.com/mw/notebook/5c1b599d8859e0002b718378

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()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值