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的副本,其中品种根据最低价格降序排序,然后是最高价格。

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)

Original: https://blog.csdn.net/qq_47997583/article/details/121980087
Author: 小帅吖
Title: Pandas学习笔记(4) Grouping and Sorting

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/700248/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球