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, 这是一个人脸识别任务,任务是识别人脸图片中的眼睛.鼻子.嘴的位置. ...
随机推荐
- 玩转 Windows Terminal
今天给大家分享一下Windows Terminal的使用及个性化定制. 一.安装 该项目的开源地址为https://github.com/microsoft/terminal,如果想折腾,可以按照上面 ...
- 数据库/MySQL的安装
来源:https://www.cnblogs.com/liubing8/p/11431382.html mysql的安装.启动和基础配置 —— windows版本 1.下载 第一步:打开网址,http ...
- flask 源码专题(一):app.run()的背后
当我们用Flask写好一个app后, 运行app.run()表示监听指定的端口, 对收到的request运行app生成response并返回. 现在分析一下, 运行app.run()后具体发生了什么事 ...
- 数据可视化之DAX篇(十七)Power BI表格总计行错误的终极解决方案
https://zhuanlan.zhihu.com/p/68183990 我在知识星球收到的问题中,关于表格和矩阵(以下统称表格)总计行错误算是常见的问题之一了,不少初学者甚为不解,在Excel透视 ...
- C#-CLR note - 26线程
开篇 async/wait的使用 static async Task Main(string[] args) { Console.WriteLine("start-- "); va ...
- Json对象,Json数组,Json字符串的区别
Json对象: var str = {"姓名":"张三","性别":"男","年龄":"2 ...
- Python Ethical Hacking - VULNERABILITY SCANNER(8)
Implementing Code To Discover XSS in Parameters 1. Watch the URL of the XSS reflected page carefully ...
- Python Ethical Hacking - WEB PENETRATION TESTING(4)
CRAWING SPIDER Goal -> Recursively list all links starting from a base URL. 1. Read page HTML. 2. ...
- 【译】GraalVM—下一代JVM介绍
原标题:GraalVM – an introduction to the next level JVM 随着Red Hat宣布Quarkus作为- 为GraalVM和HotSpot量身定制的下一代Ku ...
- ionic环境安装步骤
注:准确性有待考证,仅供参考. 1,安装jdk 配置环境变量:java_home和path2,安装node 检查版本 node -v3,安装npm:npm i cnpm -g 检查版本:cnpm -v ...