没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
试读
17页
pythonpandas基本操作练习50题 数据来源: 数据来源: ⼀、例题 ⼀、例题1:探索快餐数据 :探索快餐数据 1. 数据预览 数据预览 变量 含义 order_id 订单id quantity 数量 item_name 商品名称 choice_description 详情 item_price 商品单价 2. 代码练习 代码练习 #导⼊pandas import pandas as pd #读⼊数据 df = pd.read_csv('/Users/harper/Desktop/python/exercise_data/chipotle.tsv',sep='\t') #查看前10⾏内容 df.head(10) #数据集维度、⾏数、列数 print(df.shape)#维度:⾏*列 df.shape[0]#⾏数 df.shape[1]#⾏数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< (4622, 5) 4622 5 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< #输出列名称,可以⽐较下⾯两种形式 print(df.columns) print
资源推荐
资源详情
资源评论
pythonpandas基本操作练习50题
数据来源:数据来源:
⼀、例题⼀、例题1:探索快餐数据:探索快餐数据
1. 数据预览数据预览
变量 含义
order_id 订单id
quantity 数量
item_name 商品名称
choice_description 详情
item_price 商品单价
2. 代码练习代码练习
#导⼊pandas
import pandas as pd
#读⼊数据
df = pd.read_csv('/Users/harper/Desktop/python/exercise_data/chipotle.tsv',sep=' ')
#查看前10⾏内容
df.head(10)
#数据集维度、⾏数、列数
print(df.shape)#维度:⾏*列
df.shape[0]#⾏数
df.shape[1]#⾏数
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
(4622, 5)
4622
5
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#输出列名称,可以⽐较下⾯两种形式
print(df.columns)
print(df.columns.to_list())#以列表形式储存
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Index(['order_id', 'quantity', 'item_name', 'choice_description',
'item_price'],
dtype='object')
['order_id', 'quantity', 'item_name', 'choice_description', 'item_price']
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#被下单数最多商品(item_name)是什么
#思路:先按item_name分组,再根据quantity求和,再降序排列
#reset_index是可以输出成dataframe
df.groupby('item_name').quantity.sum().reset_index().sort_values(by = 'quantity',ascending = False)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
item_name quantity
17 Chicken Bowl 761
18 Chicken Burrito 591
25 Chips and Guacamole 506
39 Steak Burrito 386
10 Canned Soft Drink 351
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#求item_name的类别数
df['item_name'].nunique()
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
50
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#在choice_description中下单次数最多的商品是什么
#注意到choice_description 中有⼀些空值,所以我们先挑出⾮空的⾏再分组-求和-排序
df[df['choice_description'].notnull()].groupby('choice_description').quantity.sum().reset_index().sort_values(by = 'quantity',ascending = False)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
choice_description quantity
15 [Diet Coke] 159
14 [Coke] 143
583 [Sprite] 89
256 [Fresh Tomato Salsa, [Rice, Black Beans, Chees... 49
257 [Fresh Tomato Salsa, [Rice, Black Beans, Chees... 42
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#⼀共有多少商品被下单,就是计算订单总数
print(df['quantity'].sum())
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
4972
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#将item_price转换为浮点数
#注意到item_price前⾯有$符号
#⽅法1,个⼈感觉容易出错,str不知道什么时候需要加
df['item_price'] = df['item_price'].str.replace('$','')
#⽅法2:apply是针对每个元素,就不需要str了
def func(df):
df['item_price'] = df['item_price'].replace('$','')
return df
df = df.apply(func, axis = 1)
df['item_price'] = df['item_price'].astype('float')
print(df['item_price'])
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
0 2.39
1 3.39
2 3.39
3 2.39
4 16.98
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#每⼀单order对应的平均总价是多少,就是计算总收⼊/总订单数
round((df['quantity']*df['item_price']).sum()/df['order_id'].nunique(),2)
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21.39
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
⼆、例题⼆、例题2:探索欧洲杯数据:探索欧洲杯数据
1. 数据预览数据预览
2. 代码练习代码练习
#导⼊pandas
import pandas as pd
#读⼊数据
df = pd.read_csv('/Users/harper/Desktop/python/exercise_data/Euro2012_stats.csv')
#计算有多少球队参与了2012欧洲杯,就是计算Team的类别数
df['Team'].nunique()
<<<<<<<<<<<<<<<<<<<<<<
16
<<<<<<<<<<<<<<<<<<<<<<
#该数据集中⼀共有多少列
print(df.shape[1])
<<<<<<<<<<<<<<<<<<<<<<
35
<<<<<<<<<<<<<<<<<<<<<<
#将数据集中的Team,Yellow Cards和Red Cards单独存为⼀个叫discipline的数据框,对数据框按照先Red Cards再Yellow Cards进⾏排序
discipline = df[['Team','Yellow Cards','Red Cards']]
discipline.sort_values(['Red Cards','Yellow Cards'],ascending = False)
<<<<<<<<<<<<<<<<<<<<<<
Team Yellow Cards Red Cards
6 Greece 9 1
9 Poland 7 1
11 Republic of Ireland 6 1
7 Italy 16 0
7 Italy 16 0
10 Portugal 12 0
13 Spain 11 0
<<<<<<<<<<<<<<<<<<<<<<
#计算每个球队拿到的黄牌数的平均值,就是黄牌数的总数/球队总数
df['Yellow Cards'].sum()/df['Team'].nunique()
<<<<<<<<<<<<<<<<<<<<<<
7.4375
<<<<<<<<<<<<<<<<<<<<<<
#找到进球数Goals超过6的球队数据
df[df['Goals'] > 6])
<<<<<<<<<<<<<<<<<<<<<<
Team Goals Shots on target ... Subs on Subs off Players Used
5 Germany 10 32 ... 15 15 17
13 Spain 12 42 ... 17 17 18
<<<<<<<<<<<<<<<<<<<<<<
#选取以字母X开头/结尾/包含的球队数据
df[df['Team'].str.startswith('G')]#开头
<<<<<<<<<<<<<<<<<<<<<<
Team Goals Shots on target ... Subs on Subs off Players Used
5 Germany 10 32 ... 15 15 17
6 Greece 5 8 ... 12 12 20
<<<<<<<<<<<<<<<<<<<<<<
df[df['Team'].str.endswith('e')]#结尾
<<<<<<<<<<<<<<<<<<<<<<
Team Goals Shots on target ... Subs on Subs off Players Used
4 France 3 22 ... 11 11 19
6 Greece 5 8 ... 12 12 20
15 Ukraine 2 7 ... 9 9 18
<<<<<<<<<<<<<<<<<<<<<<
df[df['Team'].str.contains('a')]#中间
<<<<<<<<<<<<<<<<<<<<<<
Team Goals ... Subs off Players Used
0 Croatia 4 ... 9 16
2 Denmark 4 ... 7 15
3 England 5 ... 11 16
4 France 3 ... 11 19
5 Germany 10 ... 15 17
7 Italy 6 ... 18 19
8 Netherlands 2 ... 7 15
9 Poland 2 ... 7 17
10 Portugal 6 ... 14 16
11 Republic of Ireland 1 ... 10 17
12 Russia 5 ... 7 16
13 Spain 12 ... 17 18
15 Ukraine 2 ... 9 18
<<<<<<<<<<<<<<<<<<<<<<
#选取前7列
df.iloc[:,0:7]
#选取除了最后3列之外的全部列
df.iloc[:,0:-3]
#找到England,Italy,Russia的射正率(Shooting Accuracy)
df[df['Team'].isin(['England','Italy','Russia'])][['Team','Shooting Accuracy']]
剩余16页未读,继续阅读
资源评论
是空空呀
- 粉丝: 167
- 资源: 3万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功