1、通过loc使用isin、==或!=查询方法
#一般查询df.loc[df['column_name'] == some_value]df.loc[df['column_name'] != some_value]#查询多个值 df.loc[df['column_name'].isin(some_values)]#选择值不在some_values的行,使用~来取反df.loc[~df['column_name'].isin(some_values)]
2、根据列(column)值选择查找行(row)示例代码
import pandas as pdimport numpy as npdf = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(), 'B': 'one one two three two two one three'.split(), 'C': np.arange(8), 'D': np.arange(8) * 2})print(df)# A B C D# 0 foo one 0 0# 1 bar one 1 2# 2 foo two 2 4# 3 bar three 3 6# 4 foo two 4 8# 5 bar two 5 10# 6 foo one 6 12# 7 foo three 7 14print(df.loc[df['A'] == 'foo'])#如果要包含多个值,请将它们放在列表中,并使用isin:print(df.loc[df['B'].isin(['one','three'])])#如果希望多次执行此操作,则先创建索引然后再使用df.loc会更高效:df = df.set_index(['B'])print(df.loc['one'])#或者,要包含多个值,可以使用df.index.isin:print(df.loc[df.index.isin(['one','two'])])