0
点赞
收藏
分享

微信扫一扫

【Python|Kaggle】机器学习系列之Pandas基础练习题(五)


前言

Hello!小伙伴!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
 
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,有幸拿过一些国奖、省奖…已保研。目前正在学习C++/Linux/Python
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
 
初学Python 小白阶段
文章仅作为自己的学习笔记 用于知识体系建立以及复习
题不在多 学一题 懂一题
知其然 知其所以然!


Introduction

Run the following cell to load your data and some utility functions.

运行下面代码 导入此次练习需要的数据、库…

import pandas as pd

reviews = pd.read_csv("../input/wine-reviews/winemag-data-130k-v2.csv", index_col=0)

from learntools.core import binder; binder.bind(globals())
from learntools.pandas.data_types_and_missing_data import *
print("Setup complete.")

Exercises

1.

题目

What is the data type of the ​​points​​ column in the dataset?

解答

题目意思:

查询points的类型

dtype = reviews.points.dtype

运行结果:

【Python|Kaggle】机器学习系列之Pandas基础练习题(五)_机器学习

2.

题目

Create a Series from entries in the ​​points​​​ column, but convert the entries to strings. Hint: strings are ​​str​​ in native Python.

解答

题目意思:

将points列中的数据类型转换为string

point_strings = reviews.points.astype('str')

运行结果:

【Python|Kaggle】机器学习系列之Pandas基础练习题(五)_Python_02

3.

题目

Sometimes the price column is null. How many reviews in the dataset are missing a price?

解答

题目意思:

统计price列中nan的数量

n_missing_prices = pd.isnull(reviews.price).sum()

运行结果:

【Python|Kaggle】机器学习系列之Pandas基础练习题(五)_机器学习_03


其余参考Demo:

n_missing_prices = reviews.price.isnull().sum()

# or

missing_price_reviews = reviews[reviews.price.isnull()]
n_missing_prices = len(missing_price_reviews)

4.

题目

What are the most common wine-producing regions? Create a Series counting the number of times each value occurs in the ​​region_1​​​ field. This field is often missing data, so replace missing values with ​​Unknown​​. Sort in descending order. Your output should look something like this:

Unknown                    21247
Napa Valley 4480
...
Bardolino Superiore 1
Primitivo del Tarantino 1
Name: region_1, Length: 1230, dtype: int64

解答

题目意思:

首先需要将region_1中的空值用“unknown”填充
然后统计每一个地方的频率

reviews_per_region = reviews.region_1.fillna("Unknown").value_counts().sort_values(ascending=False)

运行结果:

【Python|Kaggle】机器学习系列之Pandas基础练习题(五)_Python_04

结语

文章仅作为学习笔记,记录从0到1的一个过程

希望对您有所帮助,如有错误欢迎小伙伴指正~

我是 海轰ଘ(੭ˊᵕˋ)੭


【Python|Kaggle】机器学习系列之Pandas基础练习题(五)_Python_05


举报

相关推荐

0 条评论