脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Pandas中Series和DataFrame的索引实现

Pandas中Series和DataFrame的索引实现

2021-07-25 00:21小舔哥 Python

这篇文章主要介绍了Pandas中Series和DataFrame的索引实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

正文

在对Series对象和DataFrame对象进行索引的时候要明确这么一个概念:是使用下标进行索引,还是使用关键字进行索引。比如list进行索引的时候使用的是下标,而dict索引的时候使用的是关键字。

使用下标索引的时候下标总是从0开始的,而且索引值总是数字。而使用关键字进行索引,关键字是key里面的值,既可以是数字,也可以是字符串等。

Series对象介绍:

Series对象是由索引index和值values组成的,一个index对应一个value。其中index是pandas中的Index对象。values是numpy中的数组对象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pandas as pd
s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd'])
print(s1)
结果:
2
3
4
5
dtype: int64
 
print(s1.index)
结果:
Index(['a', 'b', 'c', 'd'], dtype='object')
 
print(s1.values)
结果:
[2 3 4 5]

如何对Series对象进行索引?

1:使用index中的值进行索引

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
print(s1['a'])
结果:
2
 
print(s1[['a','d']])
结果:
2
5
dtype: int64
 
 
print(s1['b':'d'])
结果(注意,切片索引保存最后一个值):
3
4
5
dtype: int64

2:使用下标进行索引 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
print(s1[0])
结果:
2
 
print(s1[[0,3]])
结果:
2
5
dtype: int64
 
print(s1[1:3])
结果(注意:这里和上面不同的是不保存最后一个值,与正常索引相同):
3
4
dtype: int64

3:特殊情况:

上面的index为字符串,假如index为数字,这个时候进行索引是按照index值进行还是按照下标进行? 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
s1 = pd.Series([2,3,4,5], index=[1,2,3,4])
print(s1[2])
结果:
3
print(s1[0]) 会报错
 
print(s1[[2,4]])
结果:
2  3
4  5
dtype: int64
 
print(s1[1:3])
结果:
2  3
3  4
dtype: int64

可以看出来,当index为整数的时候,那么前两种选择是使用index的值进行索引, 而后一种切片选择使用的是下标进行索引。

4:使用布尔Series进行索引

使用布尔Series进行索引的时候,其实是要求布尔Series和我们的索引对象有相同的index。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd']
print(s1 > 3)
结果(这是一个bool Series):
False
False
c   True
d   True
dtype: bool
 
print(s1[s1 > 3])
结果(只需要把bool Series 传入Series就可以实现索引):
4
5
dtype: int64

5:使用Index对象来进行索引

使用Index对象进行索引的时候,和使用值索引没有本质的区别。因为Index里面也存入了很多值,可以把Index看做一个list。

DataFrame对象介绍:

DataFrame对象是一个由行列组成的表。DataFrame中行由columns组成,列由index组成,它们都是Index对象。它的值还是numpy数组。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
data = {'name':['ming', 'hong', 'gang', 'tian'], 'age':[12, 13, 14, 20], 'score':[80.3, 88.2, 90, 99.9]}
df1 = pd.DataFrame(data)
 
print(df1.index)
结果:
RangeIndex(start=0, stop=4, step=1)
 
print(df1.columns)
结果:
Index(['age', 'name', 'score'], dtype='object')
 
print(df1.values)
结果:
[[12 'ming' 80.3]
 [13 'hong' 88.2]
 [14 'gang' 90.0]
 [20 'tian' 99.9]]

如何对DataFrame对象进行索引

1:使用columns的值对列进行索引

  直接使用columns中的值进行索引,得到的是一列或者是多列的值

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
print(df1['name'])
结果:
0  ming
1  hong
2  gang
3  tian
Name: name, dtype: object
 
print(df1[['name','age']])
结果:
name age
0 ming  12
1 hong  13
2 gang  14
3 tian  20
注意:不可以直接使用下标对列进行索引,除非该columns当中包含该值。如下面的操作是错误的<br>print(df1[0])<br>结果: 错误

2:切片或者布尔Series对行进行索引

使用切片索引,或者布尔类型Series进行索引:

?
1
2
3
4
5
6
7
8
9
10
11
12
print(df1[0:3])
使用切片进行选择,结果:
age name score
0  12 ming  80.3
1  13 hong  88.2
2  14 gang  90.0
 
print(df1[ df1['age'] > 13 ])
使用布尔类型Series进行索引,其实还是要求布尔Series和DataFrame有相同的index,结果:
age name score
2  14 gang  90.0
3  20 tian  99.9

3:使用loc和iloc进行索引

本质上loc是用index和columns当中的值进行索引,而iloc是不理会index和columns当中的值的,永远都是用从0开始的下标进行索引。所以当你搞懂这句话的时候,下面的索引就会变得非常简单:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
print(df1.loc[3])
结果:
name   hong
score  88.2
Name: 3, dtype: object
 
print(df1.loc[:,'age'])
结果:
1  12
3  13
4  14
5  20
Name: age, dtype: int64
 
print(df1.iloc[3])
结果:
age    20
name   tian
score  99.9
Name: 5, dtype: object
 
print(df1.iloc[:,1])
结果:
1  ming
3  hong
4  gang
5  tian
Name: name, dtype: object

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/jiaxin359/p/8995133.html

延伸 · 阅读

精彩推荐