【Python|Kaggle】机器学习系列之Pandas基础练习题(四)
前言
Hello!小伙伴!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
初学Python 小白阶段
文章仅作为自己的学习笔记 用于知识体系建立以及复习
题不在多 学一题 懂一题
知其然 知其所以然!
往期推荐
【Python|Kaggle】机器学习系列之Pandas基础练习题(一)
【Python|Kaggle】机器学习系列之Pandas基础练习题(二)
【Python|Kaggle】机器学习系列之Pandas基础练习题(三)
Introduction
In these exercises we’ll apply groupwise analysis to our dataset.
Run the code cell below to load the data before running the exercises.
事先导入后面所需的数据集、库
import pandas as pdreviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)
pd.set_option("display.max_rows", 5)from learntools.core import binder; binder.bind(globals())
from learntools.pandas.grouping_and_sorting import *
print("Setup complete.")
reviews
本练习使用的数据集:
Exercises
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.
解答
题目意思:
创建一个Series,其索引是数据集中的taster_twitter_handle类别,其值计算每个人写了多少评论。
也就是先对taster_twitter_handle进行分组 然后统计每一个组的size
reviews_written = reviews.groupby('taster_twitter_handle').size()
其余参考Demo:
reviews_written = reviews.groupby('taster_twitter_handle').taster_twitter_handle.count()
Note:
- size作用与dataframe
- count作用于seriers
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).
解答
题目意思:
找出每个价格 对应评分中最高的一个
best_rating_per_price = reviews.groupby('price').points.max()
其余参考Demo:
best_rating_per_price = reviews.groupby('price')['points'].max().sort_index()
# best_rating_per_price = reviews.groupby('price')['points'].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)对应的最高价格和最低价格
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).
解答
题目意思:
统计出每一种酒(variety)对应的最高价格、最低价格 ,然后先按照最低价格进行降序排列,最低价格相同时,依据最高价格进行降序排列
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.
解答
题目意思:
统计每一个品酒师(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 MultiIndex
of {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.
解答
题目意思:
统计每一个国家(country)所具有不同酒种类(variety)的数量 按照降序排列(按照数量)
country_variety_counts = reviews.groupby(['country','variety']).size().sort_values(ascending=False)
结语
文章仅作为学习笔记,记录从0到1的一个过程
希望对您有所帮助,如有错误欢迎小伙伴指正~
我是 海轰ଘ(੭ˊᵕˋ)੭
如果您觉得写得可以的话,请点个赞吧
谢谢支持 ❤️
【Python|Kaggle】机器学习系列之Pandas基础练习题(四)相关推荐
- 【Python|Kaggle】机器学习系列之Pandas基础练习题(五)
前言 Hello!小伙伴! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 自我介绍 ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 简介:因C语言结识编程,随后转入计 ...
- python数据科学系列:pandas入门详细教程
导读 前2篇分别系统性介绍了numpy和matplotlib的入门基本知识,今天本文自然是要对pandas进行入门详细介绍,通过本文你将系统性了解pandas为何会有数据分析界"瑞士军刀&q ...
- python字典经典例题_Python小白--------基础练习题(列表,元组,字典)
Python小白--------基础练习题(列表,元组,字典) 发布时间:2018-05-16 18:12, 浏览次数:1167 , 标签: Python 今天总结了这几天所学的基础知识,做了一些练习 ...
- python报表自动化系列 - 为pandas.DataFrame制作自然数索引(更改索引为从1开始的自然数)
更改DataFrame索引为从1开始的自然数 [函数说明] 实际上对于人来说更习惯于从零开始索引单元格,这样也不会容易出错.而建立一个DataFrame变量后默认索引是从零开始的,还有一些由于如导入等 ...
- Python快速上手系列--循环结构--基础篇
本篇将讲述前几章都有提起的循环,非常的重要,对于自动化测试的脚本来说,运用的都比较的频繁.好好看. if语句 首先我们先来学习一下if语句,因为它通常会和for循环搭配使用,频率极高. if 关键字或 ...
- python学习之路4(基础练习题)
1.下面哪些是操作符,哪些是值? * 'hello' -88.8 - / + 5 答:操作符包括 :* .-./.+:值包括:'hello'.-88.8.5 2.下面哪个是变量,哪个是字符串? spa ...
- Python《机器学习实战》读书笔记(四)——朴素贝叶斯
第四章 基于概率论的分类方法朴素贝叶斯 4-1 基于贝叶斯决策理论的分类方法 4-2 条件概率 4-3 使用条件概率来分类 4-4 使用朴素贝叶斯进行文档分类 4-5 使用Python进行文本分类 4 ...
- python类的继承--------类的基础(四)
2019独角兽企业重金招聘Python工程师标准>>> class schoolmember(object): def __init__(self,name,age): self.n ...
- Weka中数据挖掘与机器学习系列之Exploer界面(七)
不多说,直接上干货! Weka的Explorer(探索者)界面,是Weka的主要图形化用户界面,其全部功能都可通过菜单选择或表单填写进行访问.本博客将详细介绍Weka探索者界面的图形化用户界面.预处理 ...
最新文章
- stm32cubemx无法生成工程_经验分享 | STM32CubeMX + STM32F1系列开发时遇到的四个问题及解决方案分享...
- 实例解说Linux中fdisk分区使用方法
- target runtime apache v7.0 not defined 的解决方法
- 如何在程序中嵌入FOP
- 医疗搜索中的query词权重算法探索
- 【转】如何把Matlab中的m文件转化成C语言代码
- PHP通过传递对象参数调用asp.net Webservice 服务
- ajax提交file文件,AjaxSubmit()提交file文件
- 海康SDK接口调用的主要流程
- CountDownLatch,CyclicBarrier,Semaphore的使用方法以及它们之间的区别
- 关于阿里云业务数据分析
- 创建pv卷报错excluded by a filter的解决办法
- 国外数据平台统计分析sdk
- fieldOfView
- linux 进程调度cfg,选择Linux I / O调度程序
- 基于java的宠物狗销售网站系统
- Cadence Allegro 查看电路板覆铜率的方法图文教程及视频演示
- Python语言零基础入门教程(十一)
- H3Cs5500堆叠配置
- 2000-2020年迪博上市公司内部控制指数