python pandas练习:数据的过滤和排序

Ex1 - Filtering and Sorting Data

参考项目地址:https://github.com/guipsamora/pandas_exercises

【Ex1 - 数据的过滤和排序】

This time we are going to pull data directly from the internet.

【这次我们直接从互联网获取数据】

Step 1. Import the necessary libraries

【第一步,导入必要的库】

import pandas as pd

Step 2. Import the dataset from this address.

【第二步,从这个地址获取数据】

Step 3. Assign it to a variable called chipo.

【将数据集赋值(assign)给变量chipo。】

sep:设置分隔符(separator),此处以一个tab(四个空格)为分隔符。

url = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv'

chipo = pd.read_csv(url, sep = '\t')

Step 4. How many products cost more than $10.00?

【第四步,有多少产品的价格大于10美元?】

1、数据清理,将item_price列中的带$符的字符串转换为浮点数。

2、删除重复项。

3、筛选价格大于10的商品,获取名称,去重之后得到大于10商品的数量。

# clean the item_price column and transform it in a float
# 清理[item_price]列的数据,将其转换成float类型。
# float(value[1 : -1])表示字符串切片,将第2位至最后一位截取出来,这里作用是将价格最前面的$符号过滤掉,只保留后面的数字。
# value的取值就是循环取后面[item_price]列的所有值,全部转换成浮点数,保存至prices这个列表里。
prices = [float(value[1 : -1]) for value in chipo.item_price]

# reassign the column with the cleaned prices
# 重新将清理过后的数据赋值给[item_price]列。
chipo.item_price = prices

# delete the duplicates in item_name and quantity
# 删除掉[item_name]列与[quantity]列中的重复项。
# 后面跟的item_name、quantity、choice_description表示参考的列名,这里表示一行里这参考的三列都重复就删除这一行。默认参考所有列。
chipo_filtered = chipo.drop_duplicates(['item_name','quantity','choice_description'])

# chipo_filtered

# select only the products with quantity equals to 1
# 筛选出数量为1的商品。
chipo_one_prod = chipo_filtered[chipo_filtered.quantity == 1]
chipo_one_prod

# 方法一:
# 使用nunique()获取指定坐轴中不同元素的数量。
# 这里显示价格大于10的商品的数量。
chipo_one_prod[chipo_one_prod['item_price']>10].item_name.nunique()
# 这里显示所有价格大于10的商品,返回一个DataFrame。
chipo_one_prod[chipo_one_prod['item_price']>10]


# 方法二: 
# 直接使用query()函数查询。函数作用是使用布尔表达式来查询DataFrame的列,最后返回的DataFrame类型的查询结果。
# 这里使用'item_price > 10'这个表达式,最后得到一个[item_price]列的值都大于10的DataFrame。再使用item_name.nunique()获取商品名称并得到名称去重之后的数量。
chipo.query('item_price > 10').item_name.nunique()
31

Step 5. What is the price of each item?

【第五步,每一个物品的价格是多少?】

【输出一个只包含[item_name]与[item_price]两列的DataFrame。】
# delete the duplicates in item_name and quantity
# 删除[item_name]与[quantity]中的重复项。
chipo_filtered = chipo.drop_duplicates(['item_name','quantity'])

# []里的是筛选条件,这里是筛选出[item_name]的值为'Chicken Bowl',并且[quantity]的值为1的数据。
chipo[(chipo['item_name'] == 'Chicken Bowl') & (chipo['quantity'] == 1)]

# select only the products with quantity equals to 1
# 筛选出数量为1的商品。
chipo_one_prod = chipo_filtered[chipo_filtered.quantity == 1]

# select only the item_name and item_price columns
# 将[item_name]与[item_price]这两列单独筛选出来。
price_per_item = chipo_one_prod[['item_name', 'item_price']]

# sort the values from the most to less expensive
# 按照价格从高到底排列。
# 使用sort_values()函数进行排序,by表示排序要参考的列,ascending=False表示降序排序,默认升序排序。
price_per_item.sort_values(by = "item_price", ascending = False).head(20)
item_name item_price
606 Steak Salad Bowl 11.89
1229 Barbacoa Salad Bowl 11.89
1132 Carnitas Salad Bowl 11.89
7 Steak Burrito 11.75
168 Barbacoa Crispy Tacos 11.75
39 Barbacoa Bowl 11.75
738 Veggie Soft Tacos 11.25
186 Veggie Salad Bowl 11.25
62 Veggie Bowl 11.25
57 Veggie Burrito 11.25
250 Chicken Salad 10.98
5 Chicken Bowl 10.98
8 Steak Soft Tacos 9.25
554 Carnitas Crispy Tacos 9.25
237 Carnitas Soft Tacos 9.25
56 Barbacoa Soft Tacos 9.25
92 Steak Crispy Tacos 9.25
664 Steak Salad 8.99
54 Steak Bowl 8.99
3750 Carnitas Salad 8.99

Step 6. Sort by the name of the item

【第六步,按照物品的名字排序】

列举了两种排序的方法。

chipo.item_name.sort_values()

# OR

chipo.sort_values(by = "item_name")
order_id quantity item_name choice_description item_price
3389 1360 2 6 Pack Soft Drink [Diet Coke] 12.98
341 148 1 6 Pack Soft Drink [Diet Coke] 6.49
1849 749 1 6 Pack Soft Drink [Coke] 6.49
1860 754 1 6 Pack Soft Drink [Diet Coke] 6.49
2713 1076 1 6 Pack Soft Drink [Coke] 6.49
... ... ... ... ... ...
2384 948 1 Veggie Soft Tacos [Roasted Chili Corn Salsa, [Fajita Vegetables,... 8.75
781 322 1 Veggie Soft Tacos [Fresh Tomato Salsa, [Black Beans, Cheese, Sou... 8.75
2851 1132 1 Veggie Soft Tacos [Roasted Chili Corn Salsa (Medium), [Black Bea... 8.49
1699 688 1 Veggie Soft Tacos [Fresh Tomato Salsa, [Fajita Vegetables, Rice,... 11.25
1395 567 1 Veggie Soft Tacos [Fresh Tomato Salsa (Mild), [Pinto Beans, Rice... 8.49

4622 rows × 5 columns

Step 7. What was the quantity of the most expensive item ordered?

【第七步,价格最高的订购的物品,它的数量有多少。】

使用sort_values()按照价格降序,再使用head(1)获取价格最高物品的信息,里面包含了数量。

chipo.sort_values(by = "item_price", ascending = False).head(1)
order_id quantity item_name choice_description item_price
3598 1443 15 Chips and Fresh Tomato Salsa NaN 44.25

Step 8. How many times was a Veggie Salad Bowl ordered?

【第八步,Veggie Salad Bowl(蔬菜沙拉碗)被订购了多少次?】

使用query()函数也能达到相同的效果。

chipo_salad = chipo[chipo.item_name == "Veggie Salad Bowl"]
# chipo_salad = chipo.query('item_name == "Veggie Salad Bowl"')

len(chipo_salad)
18

Step 9. How many times did someone order more than one Canned Soda?

【第九步,人们一次性订购Canned Soda(罐装苏打水)的数量超过1个的次数有多少?】

即item_name是"Canned Soda"并且数量(quantity)大于1的订单有多少。

这里也可以使用query()函数来查询,效果一样。

chipo_drink_steak_bowl = chipo[(chipo.item_name == "Canned Soda") & (chipo.quantity > 1)]
# chipo_drink_steak_bowl = chipo.query('item_name == "Canned Soda" & quantity > 1')

len(chipo_drink_steak_bowl)
20
posted @ 2022-04-28 10:07  鑫xin哥  阅读(362)  评论(3编辑  收藏  举报