Intndexing-selecting-assigning

教程

介绍
选择要处理的pandas DataFrame或Series的特定值是几乎将要运行的任何数据操作中的一个隐含步骤,因此在Python中处理数据时需要学习的第一件事是如何选择数据 快速有效地与您相关的要点。

如果我们有Python,则可以使用索引([])运算符访问其值。 我们可以对DataFrame中的列执行相同的操作

在Python中,我们可以通过将对象作为属性来访问它的属性。 例如,一个book对象可能具有title属性,我们可以通过调用book.title来访问它。 大熊猫DataFrame中的列的工作方式几乎相同。

因此,要访问评论的国家/地区属性,我们可以使用:

reviews.country
0            Italy
1 Portugal
...
129969 France
129970 France
Name: country, Length: 129971, dtype: object

这是从DataFrame中选择特定系列的两种方法。其他中的任何一个在语法上都没有比另一个更有效,但是索引运算符[]确实具有可以处理其中带有保留字符的列名的优点( 例如,如果我们有一个国家Providence列,则reviews.country Providence不会'工作)。

Indexing in pandas

pandas之中的索引

索引运算符和属性选择很好,因为它们的工作方式与Python生态系统的其余部分一样。 作为新手,这使它们易于拿起和使用。 但是,pandas有自己的访问运算符loc和iloc。 对于更高级的操作,这些是您应该使用的操作。

基于索引的选择

pandas索引以两种范例之一进行工作。 第一种是基于索引的选择:根据数据在数据中的数字位置选择数据。 iloc遵循此范例。
要选择DataFrame中的第一行数据,我们可以使用以下代码:

reviews.iloc[0]

pandas索引方式有以下2种

loc函数:通过行索引 "Index" 中的具体值来取行数据(如取"Index"为"A"的行)

iloc函数:通过行号来取行数据(如取第二行的数据)

loc和iloc都是第一个参数为行,第二个参数为列;这与传统Python不同

我想获取一个表格的第一列:

reviews.iloc[:, 0]

Manipulating the index

操作索引

基于标签的选择从索引中的标签获得其功能。 至关重要的是,我们使用的索引不是一成不变的。 我们可以按照我们认为合适的任何方式来操作索引。
set_index()方法可用于完成这项工作。如果您可以为数据集找到一个比当前索引更好的索引,这将很有用。

练习

1.

Select the description column from reviews and assign the result to the variable desc

# Your code here
desc = reviews["description"] # Check your answer
q1.check()

Follow-up question: what type of object is desc? If you're not sure, you can check by calling Python's type function: type(desc).

type(desc)
#q1.hint()
#q1.solution()

Output:

pandas.core.series.Series

可以看出,其是一个Series类型的变量

2.

Select the first value from the description column of reviews, assigning it to variable first_description.

first_description = reviews["description"][0]

# Check your answer
q2.check()
first_description

3.

Select the first row of data (the first record) from reviews, assigning it to the variable first_row.

first_row = reviews.loc[0,:]

# Check your answer
q3.check()
first_row

4.

Select the first 10 values from the description column in reviews, assigning the result to variable first_descriptions.

Hint: format your output as a pandas Series.

first_descriptions = reviews["description"][:10]

# Check your answer
q4.check()
first_descriptions

5.

Select the records with index labels 1, 2, 3, 5, and 8, assigning the result to the variable sample_reviews.

In other words, generate the following DataFrame:

tmp=[1,2,3,5,8]
sample_reviews = reviews.loc[tmp] # Check your answer
q5.check()
sample_reviews

6

Create a variable df containing the country, province, region_1, and region_2 columns of the records with the index labels 0, 1, 10, and 100. In other words, generate the following DataFrame:

row=[0,1,10,100]
col=["country", "province", "region_1", "region_2"]
df = reviews.loc[row,col] # Check your answer
q6.check()
df

7.

Create a variable df containing the country and variety columns of the first 100 records.

Hint: you may use loc or iloc. When working on the answer this question and the several of the ones that follow, keep the following "gotcha" described in the tutorial:

iloc uses the Python stdlib indexing scheme, where the first element of the range is included and the last one excluded. loc, meanwhile, indexes inclusively.(即iloc为python默认的索引方式,左闭右包)

This is particularly confusing when the DataFrame index is a simple numerical list, e.g. 0,...,1000. In this case df.iloc[0:1000] will return 1000 entries, while df.loc[0:1000] return 1001 of them! To get 1000 elements using loc, you will need to go one lower and ask for df.iloc[0:999].(loc

与普通python的不一样,它是左闭右闭的)

col=["country","variety"]
df = reviews.loc[0:99,col] # Check your answer
q7.check()
df

8.

Create a DataFrame italian_wines containing reviews of wines made in Italy. Hint: reviews.country equals what?

italian_wines = reviews[reviews.country=="Italy"]

# Check your answer
q8.check()

9

Create a DataFrame top_oceania_wines containing all reviews with at least 95 points (out of 100) for wines from Australia or New Zealand.

top_oceania_wines = reviews[reviews.country.isin(["Australia","New Zealand"])&(reviews.points>=95)]
# reviews.loc[reviews.country.isin(['Italy', 'France'])] # Check your answer
q9.check()
top_oceania_wines

Kaggle-pandas(2)的更多相关文章

  1. 由Kaggle竞赛wiki文章流量预测引发的pandas内存优化过程分享

    pandas内存优化分享 缘由 最近在做Kaggle上的wiki文章流量预测项目,这里由于个人电脑配置问题,我一直都是用的Kaggle的kernel,但是我们知道kernel的内存限制是16G,如下: ...

  2. kaggle入门2——改进特征

    1:改进我们的特征 在上一个任务中,我们完成了我们在Kaggle上一个机器学习比赛的第一个比赛提交泰坦尼克号:灾难中的机器学习. 可是我们提交的分数并不是非常高.有三种主要的方法可以让我们能够提高他: ...

  3. Kaggle入门教程

    此为中文翻译版 1:竞赛 我们将学习如何为Kaggle竞赛生成一个提交答案(submisson).Kaggle是一个你通过完成算法和全世界机器学习从业者进行竞赛的网站.如果你的算法精度是给出数据集中最 ...

  4. 如何使用Python在Kaggle竞赛中成为Top15

    如何使用Python在Kaggle竞赛中成为Top15 Kaggle比赛是一个学习数据科学和投资时间的非常的方式,我自己通过Kaggle学习到了很多数据科学的概念和思想,在我学习编程之后的几个月就开始 ...

  5. kaggle数据挖掘竞赛初步--Titanic<原始数据分析&缺失值处理>

    Titanic是kaggle上的一道just for fun的题,没有奖金,但是数据整洁,拿来练手最好不过啦. 这道题给的数据是泰坦尼克号上的乘客的信息,预测乘客是否幸存.这是个二元分类的机器学习问题 ...

  6. kaggle& titanic代码

    这两天报名参加了阿里天池的’公交线路客流预测‘赛,就顺便先把以前看的kaggle的titanic的训练赛代码在熟悉下数据的一些处理.题目根据titanic乘客的信息来预测乘客的生还情况.给了titan ...

  7. 初窥Kaggle竞赛

    初窥Kaggle竞赛 原文地址: https://www.dataquest.io/mission/74/getting-started-with-kaggle 1: Kaggle竞赛 我们接下来将要 ...

  8. 逻辑回归应用之Kaggle泰坦尼克之灾(转)

    正文:14pt 代码:15px 1 初探数据 先看看我们的数据,长什么样吧.在Data下我们train.csv和test.csv两个文件,分别存着官方给的训练和测试数据. import pandas ...

  9. kaggle之Grupo Bimbo Inventory Demand

    Grupo Bimbo Inventory Demand kaggle比赛解决方案集合 Grupo Bimbo Inventory Demand 在这个比赛中,我们需要预测某个产品在某个销售点每周的需 ...

  10. kaggle之人脸特征识别

    Facial_Keypoints_Detection github code facial-keypoints-detection, 这是一个人脸识别任务,任务是识别人脸图片中的眼睛.鼻子.嘴的位置. ...

随机推荐

  1. Tensorflow-gpu环境搭建

    显卡设备:英伟达1060 系统:Windows10 python版本:3.7.4 CUDA:cuda_10.0.130_411.31_win10 cudnn:cudnn-10.0-windows10- ...

  2. java 数据结构(四):java常用类四 比较器以及其他类

    比较器 1.Java比较器的使用背景: Java中的对象,正常情况下,只能进行比较:== 或 != .不能使用 > 或 < 的但是在开发场景中,我们需要对多个对象进行排序,言外之意,就需要 ...

  3. celery 错误相关:Monkey-patching not on the main thread; threading.main_thread().join() will hang from a greenlet

    /Users/wangpingyang/.pyenv/versions/3.7.2/lib/python3.7/site-packages/httprunner/__init__.py:5: Monk ...

  4. 爬虫05 /js加密/js逆向、常用抓包工具、移动端数据爬取

    爬虫05 /js加密/js逆向.常用抓包工具.移动端数据爬取 目录 爬虫05 /js加密/js逆向.常用抓包工具.移动端数据爬取 1. js加密.js逆向:案例1 2. js加密.js逆向:案例2 3 ...

  5. linux专题(六):Vim编辑器

    http://dwz.date/UDf 什么是Vim编辑器 Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用. 简单的来说, vi 是 ...

  6. java.lang.NoSuchMethodError: org.apache.poi.ss.usermodel.CellStyle.setVerticalAlignment(Lorg/apache/poi/ss/usermodel/VerticalAlignment;)V

    项目里引入了两个不同的 POI 版本 ,可能是版本冲突引起的. 但是奇怪的是 用Eclipse在本地就失败,在公共测试 环境就是OK的,同事用的 edea 编译器也是OK的. Caused by: j ...

  7. OSCP Learning Notes - File Transfers(3)

    Metasploit Attack Target Server: IE8 on WinXP 1.Start the Metasploit. setoolkit 2.Select 2)Website A ...

  8. 用Python把20年的GDP、人口以及房价数据进行了可视化

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者:CDA数据分析师 提到一线城市,大家马上会想到北上广深这四个超级大都 ...

  9. 修改虚拟机中的centos系统分辨率

    使用vmware虚拟机安装centos系统,默认分辨都很低,可使用以下方法修改虚拟机中centos系统的分辨率 1,# vi /boot/grub/grub.conf 2,找到 kernel 的那一行 ...

  10. 程序员肺被切掉一块还得去加班... 再谈“工作996,生病ICU”

    如题,为什么要说再谈“工作996,生病ICU”,因为996问题早已不是一个新问题,在我最近刚出版的新书<SOD框架“企业级”应用数据架构实战>写作期间,爆发了一次程序员“起义”,出现了一个 ...