Kaggle-pandas(2)
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:
ilocuses 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 casedf.iloc[0:1000]will return 1000 entries, whiledf.loc[0:1000]return 1001 of them! To get 1000 elements usingloc, you will need to go one lower and ask fordf.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)的更多相关文章
- 由Kaggle竞赛wiki文章流量预测引发的pandas内存优化过程分享
pandas内存优化分享 缘由 最近在做Kaggle上的wiki文章流量预测项目,这里由于个人电脑配置问题,我一直都是用的Kaggle的kernel,但是我们知道kernel的内存限制是16G,如下: ...
- kaggle入门2——改进特征
1:改进我们的特征 在上一个任务中,我们完成了我们在Kaggle上一个机器学习比赛的第一个比赛提交泰坦尼克号:灾难中的机器学习. 可是我们提交的分数并不是非常高.有三种主要的方法可以让我们能够提高他: ...
- Kaggle入门教程
此为中文翻译版 1:竞赛 我们将学习如何为Kaggle竞赛生成一个提交答案(submisson).Kaggle是一个你通过完成算法和全世界机器学习从业者进行竞赛的网站.如果你的算法精度是给出数据集中最 ...
- 如何使用Python在Kaggle竞赛中成为Top15
如何使用Python在Kaggle竞赛中成为Top15 Kaggle比赛是一个学习数据科学和投资时间的非常的方式,我自己通过Kaggle学习到了很多数据科学的概念和思想,在我学习编程之后的几个月就开始 ...
- kaggle数据挖掘竞赛初步--Titanic<原始数据分析&缺失值处理>
Titanic是kaggle上的一道just for fun的题,没有奖金,但是数据整洁,拿来练手最好不过啦. 这道题给的数据是泰坦尼克号上的乘客的信息,预测乘客是否幸存.这是个二元分类的机器学习问题 ...
- kaggle& titanic代码
这两天报名参加了阿里天池的’公交线路客流预测‘赛,就顺便先把以前看的kaggle的titanic的训练赛代码在熟悉下数据的一些处理.题目根据titanic乘客的信息来预测乘客的生还情况.给了titan ...
- 初窥Kaggle竞赛
初窥Kaggle竞赛 原文地址: https://www.dataquest.io/mission/74/getting-started-with-kaggle 1: Kaggle竞赛 我们接下来将要 ...
- 逻辑回归应用之Kaggle泰坦尼克之灾(转)
正文:14pt 代码:15px 1 初探数据 先看看我们的数据,长什么样吧.在Data下我们train.csv和test.csv两个文件,分别存着官方给的训练和测试数据. import pandas ...
- kaggle之Grupo Bimbo Inventory Demand
Grupo Bimbo Inventory Demand kaggle比赛解决方案集合 Grupo Bimbo Inventory Demand 在这个比赛中,我们需要预测某个产品在某个销售点每周的需 ...
- kaggle之人脸特征识别
Facial_Keypoints_Detection github code facial-keypoints-detection, 这是一个人脸识别任务,任务是识别人脸图片中的眼睛.鼻子.嘴的位置. ...
随机推荐
- 用nodejs实现向文件的固定位置插入内容
往文件的固定的行写入数据: 需要用到时nodejs的fs模块和path模块 用到fs模块的方法 readFileSync & writeFileSync : readFileSync 是读取文 ...
- java 基本语法(十四)Lambda (一)表达式
1.Lambda表达式使用前后的对比:举例一: @Test public void test1(){ Runnable r1 = new Runnable() { @Override public v ...
- 05 drf源码剖析之认证
05 drf源码剖析之认证 目录 05 drf源码剖析之认证 1. 认证简述 2. 认证的使用 3. 源码剖析 4. 总结 1. 认证简述 当我们通过Web浏览器与API进行交互时,我们可以登录,然后 ...
- SpringBoot2 整合JTA组件,多数据源事务管理
本文源码:GitHub·点这里 || GitEE·点这里 一.JTA组件简介 1.JTA基本概念 JTA即Java-Transaction-API,JTA允许应用程序执行分布式事务处理,即在两个或多个 ...
- Arcgis api for js实现服务端地图的增删改查
< !DOCTYPE html > <html xmlns = "http://www.w3.org/1999/xhtml" > <head > ...
- T1 找试场 题解
拖延症又犯了QwQ. 今天上午考试了,按照惯例,我仍然要把我会的所有题的题解写一遍. 1.找试场(way.cpp/in/out) 问题描述 小王同学在坐标系的(0,0)处,但是他找不到考试的试场,于是 ...
- SpringBoot整合Swagger3生成接口文档
前后端分离的项目,接口文档的存在十分重要.与手动编写接口文档不同,swagger是一个自动生成接口文档的工具,在需求不断变更的环境下,手动编写文档的效率实在太低.与swagger2相比新版的swagg ...
- 动手实现一个较为简单的MQTT服务端和客户端
项目地址:https://github.com/hnlyf168/DotNet.Framework 昨天晚上大致测试了下 ,490个客户端(一个收一个发) 平均估计每个每秒60个包 使用mqtt协 ...
- Python 3.x 安装PyQt5
一. 安装PyQt5 官方要求Python版本:Python >=3.5 打开命令行 输入 pip install PyQt5 PyQt5安装成功 安装完成功PyQt5后发现没有design ...
- HTTP request smuggling CL.TE
CL.TE 简介 前端通过Content-Length处理请求,通过反向代理或者负载均衡将请求转发到后端,后端Transfer-Encoding优先级较高,以TE处理请求造成安全问题. 检测 发送如下 ...