Python starts from scratch. Chapter 3 Data Processing and Analysis of python.query() Function

Keywords: Python

Catalog

Chapter II (pandas)

Python starts from scratch. Chapter 3 Data Processing and Analysis of dplyr (1) in Python
Python starts from scratch. Chapter 3 Data Processing and Analysis of dplyr (2) in Python
Python starts from scratch. Chapter 3 Data Processing and Analysis of dplyr (3) in Python
Python starts from scratch. Chapter 3 Data Processing and Analysis of dplyr (4) in Python
Python starts from scratch. Chapter 3 Data Processing and Analysis of dplyr (5) in Python
Python starts from scratch. Chapter 3 Data Processing and Analysis of python.query() Function

===============================================
This paper mainly introduces how to use python.query() function to manipulate the data box.

  • Building data boxes
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
Out[6]: 
  name  Gender  age  hight  weight1  weight2
0    a    male   23    173       53       53
1    n  female   24    174       74       64
2    c    male   24    164       44       54
3    d    male   22    172       62       66
4    e  female   21    161       71       81
5    f  female   20    160       60       50
  • Generally speaking, if a selection is made, the following operations can be performed:
df[df.age==24]
Out[13]: 
  name  Gender  age  hight  weight1  weight2
1    n  female   24    174       74       64
2    c    male   24    164       44       54

df[(df.age==24 )&( df.hight ==174)]
Out[14]: 
  name  Gender  age  hight  weight1  weight2
1    n  female   24    174       74       64
  • But if you use the python.query function, it's more logical and elegant
df.query("age==24")
Out[20]: 
  name  Gender  age  hight  weight1  weight2
1    n  female   24    174       74       64
2    c    male   24    164       44       54

df.query("age==24").query('hight==174')
Out[21]: 
  name  Gender  age  hight  weight1  weight2
1    n  female   24    174       74       64
Out[29]: 
  name Gender  age  hight  weight1  weight2
0    a   male   23    173       53       53
2    c   male   24    164       44       54
3    d   male   22    172       62       66
df.query('index > 2')
Out[47]: 
  name  Gender  age  hight  weight1  weight2
3    d    male   22    172       62       66
4    e  female   21    161       71       81
5    f  female   20    160       60       50
df.query('Gender =="male" and name =="a"')
Out[30]: 
  name Gender  age  hight  weight1  weight2
0    a   male   23    173       53       53

df.query('Gender =="male" and age<24')
Out[31]: 
  name Gender  age  hight  weight1  weight2
0    a   male   23    173       53       53
3    d   male   22    172       62       66
  • In addition, the query () function can also compare values between different columns:
df.query('weight1 > weight2')
Out[22]: 
  name  Gender  age  hight  weight1  weight2
1    n  female   24    174       74       64
5    f  female   20    160       60       50

Posted by superdan_35 on Mon, 08 Apr 2019 02:42:31 -0700