Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤

3.Selectingand Filtering In Pandas  使用Pandas来选择与过滤

本文是Kaggle自助学习下的文章,转回到目录点击这里



Selectingand Filtering Data选择和过滤数据

This is part of Kaggle's Learn Machine Learning series.请有条件的同学在Kaggle上自己看。(相当于免责声明了,我)。

Your dataset had too many variablesto wrap your head around, or even to print out nicely. How can you pare downthis overwhelming amount of data to something you can understand?你的数据集有太多的变量,使你无法理解,甚至无法很好地打印出来。你怎么能把这些海量的数据压缩(削减)成你能理解的东西呢?

 

To show you the techniques, we'llstart by picking a few variables using our intuition. Later tutorials will showyou statistical techniques to automatically prioritize variables.为了向你展示这些技巧,我们首先用直觉来挑选一些变量。后面的教程将向你展示统计技术来自动确定变量的优先级。

 

Before we can choosevariables/columns, it is helpful to see a list of all columns in the dataset.That is done with the columnsproperty of the DataFrame (the bottom line of code below).在选择变量/列之前,查看DataSet中所有列是很有帮助的。可以通过DataFrame的Columns属性(下面的代码)完成的。

import pandas as pd
 
melbourne_file_path = '../input/melbourne-housing-snapshot/melb_data.csv'
melbourne_data = pd.read_csv(melbourne_file_path) 
print(melbourne_data.columns)

用pd.read_csv读取文件后,使用.columns查看 结果如下(在Kaggle里使用Table可以快速选取代码)

Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤_第1张图片

There are many ways to select a subset of your data. We'llstart with two main approaches:有许多方法可以选择数据的子集。我们将从两种主要方法开始:

Selecting aSingle Column选择一列

       Youcan pull out any variable (or column) with dot-notation.This single column is stored in a Series,which is broadly like a DataFrame with only a single column of data. Here's anexample: 你可以使用dot-notation(点方法)提取任何变量(或列)。这一列存储在Series中,它大致就像一个只有一列数据的DataFrame。下面是一个例子:

# store the series of prices separately as melbourne_price_data.
melbourne_price_data = melbourne_data.Price
# the head command returns the top few lines of data.
print(melbourne_price_data.head())

 

这里使用直接data.head()方法,提出的是数据集里前五列。

 Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤_第2张图片


将data中的SalePrice放到data_salePrice中再使用.head()方法

 Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤_第3张图片

所以 如果要提取某一列 直接数据集使用点方法+其名字(像属性一样)

data_salePrice= data.SalePrice;

提取前五行使用data.head();

 

SelectingMultiple Columns 选择多个列

       Youcan select multiple columns from a DataFrame by providing a list of columnnames inside brackets. Remember, each item in that list should be a string(with quotes). 通过提供括号内的列名称列表,你可以从DataFrame中选择多个列。请记住,该列表中的要选择的列都应该是一个字符串(带引号)。

columns_of_interest = ['Landsize', 'BuildingArea']
two_columns_of_data = melbourne_data[columns_of_interest]

    这个语法应该在Java里没有吧qwq 起码我没接触到过..先来个字符串列表(等于Java的数组),再将这个数组作为参数。


 


如图:

Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤_第4张图片
We can verify that we got the columns we need with the describe command. 我们可以使用Description命令验证我们是否获得了所需的列。

 Kaggle学习 Learn Machine Learning 3.Selecting and Filtering In Pandas 使用Pandas来选择与过滤_第5张图片

Your Turn 该你啦!

In the notebook with yourcode:在notebook上写上你的代码:

1.   Print a list of the columns 打印列表

2.   From the list of columns, find aname of the column with the sales prices of the homes. Use the dot notation toextract this to a variable (as you saw above to create melbourne_price_data.)从列表中找到包含房屋销售价格的列的名称。使用点符号将其赋值到一个变量中(如上所示,以创建melbourne_price_data。)

3.   Use the head commandto print out the top few lines of the variable you just created.使用head命令打印出刚才创建的变量的前几行。

4.    Pickany two variables and store them to a new DataFrame (as you saw above tocreate two_columns_of_data.) 选择任意两个变量并将它们存储到一个新的DataFrame中(正如你在上面看到的那样创建two_columns_of_data。)

5.   Use the describe command with theDataFrame you just created to see summaries of those variables. 使用describe命令和刚刚创建的DataFrame来查看这些变量的摘要。

这些在上面的代码示例中已经展示了,就不再贴代码了。

 

Continue

Now that you can specify what data you want for a model,you are ready to  build your first model in the next step.现在你可以指定模型所需的数据,在下一步中将构建你的第一个模型。


 

本文是Kaggle自助学习下的文章,转回到目录点击这里

你可能感兴趣的:(Kaggle)