Selecting with complex criteria from a Pandas DataFrame

PythonServer Side ProgrammingProgramming

We can use different criteria to compare all the column values of a Pandas DataFrame. We can perform comparison operations like df[col]<5, df[col]==10, etc. For example, if we use the criteria df[col]>2, then it will check all the values from col and compare whether they are greater than 2. For all the column values, it will return True if the condition holds, else False. Let's take an example and see how it's done.

Steps

  • Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
  • Print the input DataFrame, df.
  • Initialize a variable col, with a column name.
  • Perform some comparison operations.
  • Print the resultant DataFrame.

Example

import pandas as pd

df = pd.DataFrame(
     {
        "x": [5, 2, 7, 0],
        "y": [4, 7, 5, 1],
        "z": [9, 3, 5, 1]
     }
)
print "Input DataFrame is:\n", df

col = "x"
print "Elements > 5 in column ", col, ":\n", df[col] > 5
print "Elements == 5 in column ", col, ":\n", df[col] == 5

col = "y"
print "Elements < 5 in column ", col, ":\n", df[col] < 5
print "Elements != 5 in column ", col, ":\n", df[col] != 5

Output

Input DataFrame is:
   x  y  z
0  5  4  9
1  2  7  3
2  7  5  5
3  0  1  1

Elements > 5 in column x :
0  False
1  False
2  True
3  False
Name: x, dtype: bool

Elements == 5 in column x :
0  True
1  False
2  False
3  False
Name: x, dtype: bool

Elements < 5 in column y :
0  True
1  False
2  False
3  True
Name: y, dtype: bool

Elements != 5 in column y :
0  True
1  True
2  False
3  True
Name: y, dtype: bool
raja
Published on 14-Sep-2021 12:40:37
Advertisements