python.query()函数

python 专栏收录该内容
59 篇文章 1 订阅
#使用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])

  • 4
    点赞
  • 1
    评论
  • 45
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值