温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Pandas中Series和DataFrame的索引实现

发布时间:2021-06-03 16:32:44 来源:亿速云 阅读:171 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关Pandas中Series和DataFrame的索引实现,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

Series对象介绍:

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

import pandas as pd
s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd'])
print(s1)
结果:
a  2
b  3
c  4
d  5
dtype: int64

print(s1.index)
结果:
Index(['a', 'b', 'c', 'd'], dtype='object')

print(s1.values)
结果:
[2 3 4 5]

如何对Series对象进行索引?

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

print(s1['a'])
结果:
2

print(s1[['a','d']])
结果:
a  2
d  5
dtype: int64


print(s1['b':'d'])
结果(注意,切片索引保存最后一个值):
b  3
c  4
d  5
dtype: int64

2:使用下标进行索引 

print(s1[0])
结果:
2

print(s1[[0,3]])
结果:
a  2
d  5
dtype: int64

print(s1[1:3])
结果(注意:这里和上面不同的是不保存最后一个值,与正常索引相同):
b  3
c  4
dtype: int64

3:特殊情况:

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

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。

s1 = pd.Series([2,3,4,5], index=['a', 'b', 'c', 'd']
print(s1 > 3)
结果(这是一个bool Series):
a  False
b  False
c   True
d   True
dtype: bool

print(s1[s1 > 3])
结果(只需要把bool Series 传入Series就可以实现索引):
c  4
d  5
dtype: int64

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

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

DataFrame对象介绍:

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

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中的值进行索引,得到的是一列或者是多列的值

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当中包含该值。如下面的操作是错误的
print(df1[0])
结果: 错误

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

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

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开始的下标进行索引。所以当你搞懂这句话的时候,下面的索引就会变得非常简单:

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

看完上述内容,你们对Pandas中Series和DataFrame的索引实现有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI