Pandas in the Series Index Usage

This is a summary of the most basic Pandas usage, but also to facilitate their review and conduct inquiries in the future.
Previous article summarizes the method of creating and DataFrame Series, but it ignores the index of this important concept. In this article today to take stock of what Series indexing problems.

1. What is the Index

1.1 understanding Index

First blog post using the above method to create a simple Series.

s1 = pd.Series(['a', 'b', 'c', 'd'])
print(s1)
-----------------------------
[out]: 
0    a
1    b
2    c
3    d
dtype: object

We said, s1 from the left-most column of numbers that are called zero index (index). Right side of each index corresponds to a value (value). Let's take a look at how all of the output and all of the index value.

print(s1.index)     # 由于是默认生成的索引,输出就是这个样子
[out]: 
RangeIndex(start=0, stop=4, step=1)
-----------------------------------------
print(s1.values)    # 输出为Numpy中的数组类型
[out]: 
array(['a', 'b', 'c', 'd'], dtype=object)      

1.2 custom indexes

Of course, we can use when creating Series of index This parameter specifies the index of yourself looks like.

s2 = pd.Series(['a', 'b', 'c', 'd'], index = ['one', 'two', 'three', 'four'])
print(s2)
-----------------------------
[out]:
one      a
two      b
three    c
four     d
dtype: object

If at this time the output value and the value of the index:

print(s1.index)      # 此时就会输出我们自定义的索引
[out]: 
Index(['one', 'two', 'three', 'four'], dtype='object')
-----------------------------
print(s2.values)
[out]:
array(['a', 'b', 'c', 'd'], dtype=object)

2. how to use index

2.1 Location Index

Series whole sequence is the presence of a predetermined position, the position of the first number with zero flag, a flag with the second number, and so on. So we can use this index to access location.

# 对于s2,输出是一毛一样的
print(s1[2])     # 取一个值
[out]: 'c'
-------------------------------------------
print(s1[[0, 3]])   # 取多个值,用列表括起来
[out]: 
0    a
3    d
dtype: object

2.2 Use index labels

For s2, we not only have positional order, as well as our custom index value ah (also called label) and, therefore, the birth of the index using the label.

s2['two']      # 取一个值
[out]: 'b'
-----------------------------------------------
s2[['one', 'three']]    # 取多个值,用列表括起来
[out]:
one      a
three    c
dtype: object

2.3 Use slice index

The previous example is a single index value (index list even if take multiple values, but also an index for the list to find a corresponding value). It's like a whole bread, which pulling piece to eat, then pulling one to eat.
But bread can be cut directly down to eat a whole slice it, this use of the notion of a slice, that directly take Series in a block of contiguous data out.

# 对位置索引进行切片
print(s2[1:3])      # 使用位置索引,是不包含最后一个位置3对应的值滴
-----------------------------
[out]: 
two      b
three    c

# 对标签索引进行切片
print(s2['two':'four'])    # 使用标签索引,却可以包含最后一个标签对应的值
-----------------------------
[out]: 
two      b
three    c
four     d
dtype: object

2.3 Boolean value index

s3 = pd.Series([89, 92, 70, 95], index = ['小明', '小红', '小兰', '小花'])
-----------------------------
[out]: 
小明    89
小红    92
小兰    70
小花    95
dtype: int64

We want to elect people score greater than 90. First need to determine whether each person's score is greater than 90:

print(s3 > 90)
-----------------------------
[out]: 
小明    False
小红     True
小兰    False
小花     True
dtype: bool

And then use the resulting Boolean series can extract all the determination result is True part:

print(s3[s3 > 90])
-----------------------------
[out]: 
小红    92
小花    95
dtype: int64
Released four original articles · won praise 30 · views 4465

Guess you like

Origin blog.csdn.net/Fantine_Deng/article/details/104724589