While working on a dataset we sometimes need to search foe some values in a features and for that values we need to get the values form another features. It may looks very complicated but its very simple with the help of python.
This python source code does the following:
1. Creates data dictionary and converts it into dataframe
2. Uses "where" function to filter out desired data columns
So this is the recipe on how we search a value within a Pandas DataFrame column.
import pandas as pd
We have only imported pandas which is needed.
We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'.
raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'],
'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'],
'age': [42, 38, 36, 41, 35],
'Comedy_Score': [9, 7, 8, 8, 5],
'Rating_Score': [25, 25, 49, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age',
'Comedy_Score', 'Rating_Score'])
print(df)
We are searching the data in the feature Rating_Score which have values less than 50 and for those values we are selecting the coresponding values in comedy_Score.
print(df['Comedy_Score'].where(df['Rating_Score'] < 50))
So the output comes as
first_name last_name age Comedy_Score Rating_Score 0 Sheldon Copper 42 9 25 1 Raj Koothrappali 38 7 25 2 Leonard Hofstadter 36 8 49 3 Howard Wolowitz 41 8 62 4 Amy Fowler 35 5 70 0 9.0 1 7.0 2 8.0 3 NaN 4 NaN Name: Comedy_Score, dtype: float64