pandas实现选取特定索引的行


本文摘自php中文网,作者不言,侵删。

下面为大家分享一篇pandas实现选取特定索引的行,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧

如下所示:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

>>> import numpy as np

>>> import pandas as pd

>>> index=np.array([2,4,6,8,10])

>>> data=np.array([3,5,7,9,11])

>>> data=pd.DataFrame({'num':data},index=index)

>>> print(data)

  num

2   3

4   5

6   7

8   9

10  11

>>> select_index=index[index>5]

>>> print(select_index)

[ 6 8 10]

>>> data['num'].loc[select_index]

6   7

8   9

10  11

Name: num, dtype: int32

>>>

注意,不能用iloc,iloc是将序列当作数组来访问,下标又会从0开始:

1

2

3

4

5

6

7

8

9

10

11

>>> data['num'].iloc[2:5]

6   7

8   9

10  11

Name: num, dtype: int32

>>> data['num'].iloc[[2,3,4]]

6   7

8   9

10  11

Name: num, dtype: int32

>>>

相关推荐:

基于pandas数据样本行列选取的方法

pandas groupby 分组取每组的前几行记录方法

以上就是pandas实现选取特定索引的行的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

Python输代码怎么换行?

如何找到一个目录下所有.txt文件

使用Python3+xlrd解析excel的实例

Python如何生成随机密码

Python基础入门--函数

Python怎么改shell界面的背景

作业登录接口总结

Python有switch语句吗

Python字符串与字典相关操作的详解

基于numpy.random.randn()与rand()的区别详解

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...