很多情况下,我们会根据一个dataframe里面的值来查找而不是根据索引来查找。

首先我们创建一个dataframe:

>>> col = ["id","name","sex","age"]

>>> name = {1:"chen",2:"wang",3:"hu",4:"lee",5:"liu"}
>>> id = range(1,6)
>>> sex = {1:1,2:0,3:1,4:1,5:0}
>>> age = {1:20,2:18,3:21,4:20,5:18}
>>> data = {"id":id,"name":name,"sex":sex,"age":age} >>> data
{'sex': {1: 1, 2: 0, 3: 1, 4: 1, 5: 0}, 'age': {1: 20, 2: 18, 3: 21, 4: 20, 5: 18}, 'name': {1: 'chen', 2: 'wang', 3: 'hu', 4: 'lee', 5: 'liu'}, 'id': range(1, 6)} >>> df = pd.DataFrame(data,columns=col,index=id)
>>> df
id name sex age
1 1 chen 1 20
2 2 wang 0 18
3 3 hu 1 21
4 4 lee 1 20
5 5 liu 0 18 >>> df = df.set_index("id") >>> df.set_index("id")
name sex age
id
1 chen 1 20
2 wang 0 18
3 hu 1 21
4 lee 1 20
5 liu 0 18

如果我们要选年龄大于等于20岁的,这个好办:

>>> df[df["age"]>=20]
name sex age
id
1 chen 1 20
3 hu 1 21
4 lee 1 20

或者选出所有女生(sex=0的),也好办:

>>> df[df["sex"]==0]
name sex age
id
2 wang 0 18
5 liu 0 18

也可用where,但不太方便:(一般不会这样用)

>>> df.where(df["sex"]==0)
name sex age
id
1 NaN NaN NaN
2 wang 0.0 18.0
3 NaN NaN NaN
4 NaN NaN NaN
5 liu 0.0 18.0
>>> df.where(df["age"]>=20)
name sex age
id
1 chen 1.0 20.0
2 NaN NaN NaN
3 hu 1.0 21.0
4 lee 1.0 20.0
5 NaN NaN NaN

但是如果要按名字来选出,就不能这样了,得用.isin()方法。

>>> select_name = ["chen","lee","liu"]

>>> df[df["name"]==select_name]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Python3\lib\site-packages\pandas\core\ops.py", line 855, in wrapper
res = na_op(values, other)
File "E:\Python3\lib\site-packages\pandas\core\ops.py", line 759, in na_op
result = _comp_method_OBJECT_ARRAY(op, x, y)
File "E:\Python3\lib\site-packages\pandas\core\ops.py", line 737, in _comp_method_OBJECT_ARRAY
result = lib.vec_compare(x, y, op)
File "pandas\lib.pyx", line 868, in pandas.lib.vec_compare (pandas\lib.c:15418)
ValueError: Arrays were different lengths: 5 vs 3
# 可以看到匹配会出错 >>> df[df["name"].isin(select_name)]
name sex age
id
1 chen 1 20
4 lee 1 20
5 liu 0 18

如果要选出既是属于名字里的又是男生(sex=1):

>>> df[df["name"].isin(select_name) & df["sex"]==1]
name sex age
id
1 chen 1 20
4 lee 1 20

这里如果用

>>> df.isin({"name":select_name,"sex":[1]})
name sex age
id
1 True True False
2 False False False
3 False True False
4 True True False
5 True False False >>> df[df.isin({"name":select_name,"sex":[1]})] # 这里得是[1],非1
name sex age
id
1 chen 1.0 NaN
2 NaN NaN NaN
3 NaN 1.0 NaN
4 lee 1.0 NaN
5 liu NaN NaN

好像并不好。

dataframe按值(非索引)查找多行的更多相关文章

  1. linux shell脚本查找重复行/查找非重复行/去除重复行/重复行统计

    转自:http://blog.sina.com.cn/s/blog_6797a6700101pdm7.html 去除重复行 sort file |uniq 查找非重复行 sort file |uniq ...

  2. Pandas 如何通过获取双(多)重索引获取指定行DataFrame数据

    图片看不清楚的话,可以右键选择:“在新标签页中打开图片(I)” 参数 df.loc[(a,b),c]中第一个参数元组为索引内容,a为level0索引对应的内容,b为level1索引对应的内容 因为df ...

  3. Oracle中NULL值与索引

    NULL值是关系数据库系统布尔型(true,false,unknown)中比较特殊类型的一种值,通常称为UNKNOWN或空值,即是未知的,不确定的.由于NULL存在着无数的可能,因此NULL值也不等于 ...

  4. pandas 对数据帧DataFrame中数据的索引及切片操作

    1.创建数据帧 index是行索引,即每一行的名字:columns是列索引,即每一列的名字.建立数据帧时行索引和列索引都需要以列表的形式传入. import pandas as pd df = pd. ...

  5. 非索引列上的统计 <第二篇>

    非索引列上的统计 有时候,可能在连接或过滤条件中的列上没有索引.即使对这种非索引列,如果查询优化器知道这些列的数据分布(统计),它也很可能做出最佳的选择. 除了索引上的统计,SQL Server可以在 ...

  6. 第十二章——SQLServer统计信息(2)——非索引键上统计信息的影响

    原文:第十二章--SQLServer统计信息(2)--非索引键上统计信息的影响 前言: 索引对性能方面总是扮演着一个重要的角色,实际上,查询优化器首先检查谓词上的统计信息,然后才决定用什么索引.一般情 ...

  7. 在DataFrame数据表里面提取需要的行

    在DataFrame数据表里面提取需要的行 代码功能: 在DataFrame表格中使用loc(),得到我们想要的行,然后根据某一列元素的值进行排序 此代码中还展示了为DataFrame添加列,即直接n ...

  8. 找出numpy array数组的最值及其索引

    在list列表中,max(list)可以得到list的最大值,list.index(max(list))可以得到最大值对应的索引 但在numpy中的array没有index方法,取而代之的是where ...

  9. 3.MySQL优化---单表查询优化的一些小总结(非索引设计)

    整理自互联网.摘要: 接下来这篇是查询优化.其实,大家都知道,查询部分是远远大于增删改的,所以查询优化会花更多篇幅去讲解.本篇会先讲单表查询优化(非索引设计).然后讲多表查询优化.索引优化设计以及库表 ...

  10. Sublime文本排序&查找重复行&删除重复行

    排序 按F9或者选择菜单:Edit > Sort Lines,对每行文本进行排序 查找重复行 排序好后,按Ctrl+F,调出查找面板 查找字符串: ^(.+)$[\r\n](^\1$[\r\n] ...

随机推荐

  1. A Secret(KMP)

    A Secret Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)Total ...

  2. eclipse java文件提示 The import XXX cannot be resolved

    问题:eclipse导入类   提示The import XXX cannot be resolved 原因:原来使用JDK和现在使用的JDK不同造成的buildpath不对 解決方法: 1.右键项目 ...

  3. p:nth-last-child(2)

    <!DOCTYPE html><html><head><style> p:nth-last-child(2){background:#ff0000;}& ...

  4. mesos cluster

    http://spark.apache.org/docs/latest/running-on-mesos.html http://stackoverflow.com/questions/1993985 ...

  5. 如何使用 Opencv dnn 模块调用 Caffe 预训练模型?

    QString modelPrototxt = "D:\\Qt\\qmake\\CaffeModelTest\\caffe\\lenet.prototxt"; QString mo ...

  6. jenkins配置SVN报错

    jenkins配置SVN报错,如图:

  7. Python如何实现单例模式?其他23种设计模式python如何实现?

    #使用__metaclass__(元类)的高级python用法 class Singleton2(type): def __init__(cls, name, bases, dict): super( ...

  8. linux 文件和文件夹的ctime,mtime,atime的差别

    多了不再赘述,看以下解释 st_atime            Time when file data was last accessed. Changed by  the            f ...

  9. android自定义控件(四)坐标系

    1.局部坐标系(Local Coordinate) 所谓本地坐标系,就是坐标系以物体的中心为坐标原点,物体旋转.平移等操作都是围绕局部坐标系进行的.这时当物体模型进行旋转.平移等操作时, 局部坐标系也 ...

  10. (转)fiddler使用简介--其三

    原文地址:http://www.cnblogs.com/miantest/p/7294620.html 我们知道Fiddler是位于客户端和服务器之间的代理,它能够记录客户端和服务器之间的所有 HTT ...