In Python Pandas multiple rows of data are selected according to the values of columns

  • 2021-07-10 20:26:06
  • OfStack

Selecting Multiple Rows of Data Based on Column Values in Pandas


#  Select a row record equal to some value   Use  == 
df.loc[df['column_name'] == some_value]
#  Select whether a column is a 1 Numeric value of type   Use  isin
df.loc[df['column_name'].isin(some_values)]
#  Selection of various conditions   Use  &
df.loc[(df['column'] == some_value) & df['other_column'].isin(some_values)]
#  Select row records that are not equal to certain values   Use   ! =
df.loc[df['column_name'] != some_value]
# isin Return 1 Numerical value of series , If you want to select a value that does not meet this condition, use the ~
df.loc[~df['column_name'].isin(some_values)]
import pandas as pd 
import numpy as np
df = 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 14
print(df.loc[df['A'] == 'foo'])
   A   B C  D
0 foo  one 0  0
2 foo  two 2  4
4 foo  two 4  8
6 foo  one 6 12
7 foo three 7 14
#  If you want to include multiple values, put them in the 1 A list Inside, and then use isin
print(df.loc[df['B'].isin(['one','three'])])
   A   B   C  D
0 foo  one 0  0
1 bar  one 1  2
3 bar three 3  6
6 foo  one 6 12
7 foo three 7 14
df = df.set_index(['B'])
print(df.loc['one'])
 A  B  C   D
one foo 0  0
one bar 1  2
one foo 6 12
A  B  C  D  
one foo 0  0
one bar 1  2
two foo 2  4
two foo 4  8
two bar 5  10
one foo 6  12

Summarize


Related articles: