Recipe: How to search a value within a Pandas DataFrame column?
DATA MUNGING PYTHON PANDAS DATAFRAME PANDAS CHEATSHEET PANDAS DATAFRAME TUTORIAL PANDAS COLUMN

How to search a value within a Pandas DataFrame column?

This recipe helps you search a value within a Pandas DataFrame column
In [1]:
## How to search a value within a Pandas DataFrame column
def Snippet_106():
    print()
    print(format('How to search a value within a Pandas DataFrame column','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    import pandas as pd
    raw_data = {'first_name': ['Jason', 'Jason', 'Tina', 'Jake', 'Amy'],
                'last_name': ['Miller', 'Miller', 'Ali', 'Milner', 'Cooze'],
                'age': [42, 42, 36, 24, 73],
                'preTestScore': [4, 4, 31, 2, 3],
                'postTestScore': [25, 25, 57, 62, 70]}

    df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age',
                                           'preTestScore', 'postTestScore'])
    print(); print(df)

    # Find where a value exists in a column
    # View preTestscore where postTestscore is greater than 50
    print(); print(df['preTestScore'].where(df['postTestScore'] > 50))

Snippet_106()
**************How to search a value within a Pandas DataFrame column**************

  first_name last_name  age  preTestScore  postTestScore
0      Jason    Miller   42             4             25
1      Jason    Miller   42             4             25
2       Tina       Ali   36            31             57
3       Jake    Milner   24             2             62
4        Amy     Cooze   73             3             70

0     NaN
1     NaN
2    31.0
3     2.0
4     3.0
Name: preTestScore, dtype: float64


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
2 developers from HCL
1 developer from eClerx
1 developer from HvH
1 developer from Vodafone
1 developer from Altimetrik
1 developer from Ericsson
1 developer from ICU Medical
1 developer from ANAC
1 developer from FIA Technology Services
1 developer from Netcore