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 1235, 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 countryprovinceregion_1, and region_2 columns of the records with the index labels 0110, 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
原文地址:https://www.cnblogs.com/caishunzhe/p/13406432.html