Pandas学习笔记(4) Grouping and Sorting

1.Who are the most common wine reviewers in the dataset? Create a Series whose index is the taster_twitter_handle category from the dataset, and whose values count how many reviews each person wrote.

按照taster_twitter_handle的数量排序

reviews_written = reviews.groupby("taster_twitter_handle").taster_twitter_handle.count()

2.What is the best wine I can buy for a given amount of money? Create a Series whose index is wine prices and whose values is the maximum number of points a wine costing that much was given in a review. Sort the values by price, ascending (so that 4.0 dollars is at the top and 3300.0 dollars is at the bottom).

一定的钱买到最好的酒。按照price作为index然后按照points从大到小排序

best_rating_per_price = reviews.groupby('price').points.agg(max)

3.What are the minimum and maximum prices for each variety of wine? Create a DataFrame whose index is the variety category from the dataset and whose values are the min and max values thereof.

按照variety作为索引对价格按照max和min进行排序

price_extremes = reviews.groupby('variety').price.agg([min,max])

4.What are the most expensive wine varieties? Create a variable sorted_varieties containing a copy of the dataframe from the previous question where varieties are sorted in descending order based on minimum price, then on maximum price (to break ties).

最昂贵的葡萄酒品种有哪些? 创建一个变量sorted_varieties,其中包含前一个问题的DataFrame的副本,其中品种根据最低价格降序排序,然后是最高价格。

sorted_varieties = price_extremes.sort_values(by=['min','max'],ascending=False)

5.Create a Series whose index is reviewers and whose values is the average review score given out by that reviewer. Hint: you will need the taster_name and points columns.

创建一个Series,其索引是审阅者,其值是该审阅者给出的平均评审分数。 提示:您将需要taster_name和points列。

reviewer_mean_ratings = reviews.groupby('taster_name').points.mean()

6.What combination of countries and varieties are most common? Create a Series whose index is a MultiIndexof {country, variety} pairs. For example, a pinot noir produced in the US should map to {"US", "Pinot Noir"}. Sort the values in the Series in descending order based on wine count.

哪个国家和品种的组合最常见? 创建一个索引为MultiIndexof {country, variety}对的系列。 例如,在美国生产的黑皮诺葡萄酒应该映射为{“US”, " pinot noir "}。 根据葡萄酒计数以降序排列级数中的值。

country_variety_counts = reviews.groupby(['country','variety']).size().sort_values(ascending=False)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值