[code]df['column_name'] >= (A & df['column_name']) 要选择列值不等于 some_value的行,请使用!=:[code]df.loc[df['column_name'] != some_value] % x; Q. P% n: ^- C! L& N7 o
isin回到布尔系列,所以选择值不在的行some_values,请使用以下命令否定布尔系列~: * L7 r5 V) U) I" }, V6 D' L
df.loc[~df['column_name'].isin(some_values)] S6 H& I+ E5 p' m( R9 h$ A
例如, 4 S/ v9 j- g! _! S; \
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']) % {" p: x# c! P; l/ M6 Q& ^2 H
产量 9 l$ D/ [+ _& T: z6 {$ @& \4 f
A B C D0 foo one 00 02 foo two 2 44 44foo two 4 86 86 foo one 6 127 foo three 7 14 # y) o8 f0 O& l+ A* z% w
如果您想包含多个值,请将它们放在列表中(或更一般地说,任何可迭代)并使用isin: ! V1 A X$ g6 }$ L
' C% I' |# j5 {/ H
print(df.loc[df['B'].isin(['one','three】code]产量[code] A B C D0 foo one 00 01 bar one 1 23 bar three 3 66 foo one 6 127 foo three 7 14 ^4 y" l/ z* z8 }9 ]: T& _9 g0 C
但请注意,如果您想多次执行此操作,请先创建索引,然后使用它df.loc:! k# W( {3 m8 T- s% E
df = df.set_index(['B'])print(df.loc['one'])* [3 j4 X! z8 h$ P4 F
产量 " G1 q: g" o @$ k- X; A& ^
A C DB one foo 0 0one bar 1 2one foo 6 12& ^1 `( S7 h. W; x2 ]& @
或者,请使用索引中的多个值df.index.isin:+ Y5 o. @$ j Z6 @
2 K3 B8 Y' P1 H+ q
df.loc[df.index.isin(['one','two】code]产量[code] A C DB one foo 0 0one bar 1 2two foo 2 4two foo 4 8two bar 5 10one foo 6 12 6 W5 L$ G' T. {# ^* O