[python] pandas study notes (two) five ways to query values

1. df.loc method, query according to the label value of the row and column
2. df.iloc method, query according to the number position of the row and column
3. df.where method
4. df.query method

loc method (applicable to both rows and columns):
(1) Use separate label query method
(2) Use value list batch query
(3) Use numeric interval for range query
(4) Use conditional expression
(5) call function

import pandas as pd

#根据多个字典序列创建列表
data = {
    
    
    'sh':[1,2,3],
    'sg':[2,2,2],
    'sj':[8,8,8]
}

a = pd.DataFrame(data)

print(a)
print("-----------------------------------------------------")

#得到单个值
print(a.loc[1,'sj'])

#得到某一行
print(a.loc[0, 'sh':'sj'])

#得到某范围
print(a.loc[[0,2],['sg','sj']])

#条件表达式,可用&、|
print(a.loc[a['sh']>=2,:])

#直接写lambda表达式
print(a.loc[lambda a:a['sh']>=2,:])

Guess you like

Origin blog.csdn.net/Sgmple/article/details/113033058