#使用python.query()函数对数据框进行(挑选行)的操作
import pandas as pd
d={
    'name':['a','n','c','d','e','f'],
    'Gender':['male','female','male','male','female','female'],
    'age':[23,24,24,22,21,20],
    'hight':[173,174,164,172,161,160],
    'weight1':[53,74,44,62,71,60],
    'weight2':[53,64,54,66,81,50]
}
df=pd.DataFrame(d)
df
nameGenderagehightweight1weight2
0amale231735353
1nfemale241747464
2cmale241644454
3dmale221726266
4efemale211617181
5ffemale201606050
df[df.age==24]
nameGenderagehightweight1weight2
1nfemale241747464
2cmale241644454
df[(df.age==24 )&( df.hight ==174)]
nameGenderagehightweight1weight2
1nfemale241747464
df.query("age==24")
nameGenderagehightweight1weight2
1nfemale241747464
2cmale241644454
df.query("age==24").query('hight==174')
nameGenderagehightweight1weight2
1nfemale241747464
df.query('index > 2')
nameGenderagehightweight1weight2
3dmale221726266
4efemale211617181
5ffemale201606050
df.query('Gender =="male" and name =="a"')
nameGenderagehightweight1weight2
0amale231735353
df.query('Gender =="male" and age<24')
nameGenderagehightweight1weight2
0amale231735353
3dmale221726266
import numpy as np

grade = np.array([1, 3,  4, 5, 0,  2, -1])

grade
array([ 1,  3,  4,  5,  0,  2, -1])
grade1 = np.where(grade > 3)

grade1#注意它返回的是数据的索引位置。
(array([2, 3], dtype=int32),)
grade1 = np.where(grade > 3, 'high', 'low')
#返回最小值的索引,返回最大值的索引,排序后的索引。
grade.argmin()
6
 grade.argmax()
3
grade.argsort()
array([6, 4, 0, 5, 1, 2, 3], dtype=int32)
grade = np.array([1, 3,  4, 5, 0,  2, -1])

grade1 = np.array([1, 3,  4, 5])

np.intersect1d(grade, grade1)#返回实际值
array([1, 3, 4, 5])

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐