How to select or filter rows from a DataFrame based on values in columns in pandas?
Basic ways to select rows from a pandas dataframe:
import pandas as pd employees = pd.DataFrame({ 'EmpCode': ['Emp001', 'Emp002', 'Emp003', 'Emp004', 'Emp005'], 'Name': ['John', 'Doe', 'William', 'Spark', 'Mark'], 'Occupation': ['Chemist', 'Statistician', 'Statistician', 'Statistician', 'Programmer'], 'Date Of Join': ['2018-01-25', '2018-01-26', '2018-01-26', '2018-02-26', '2018-03-16'], 'Age': [23, 24, 34, 29, 40]}) print("\nUse == operator\n") print(employees.loc[employees['Age'] == 23]) print("\nUse < operator\n") print(employees.loc[employees['Age'] < 30]) print("\nUse != operator\n") print(employees.loc[employees['Occupation'] != 'Statistician']) print("\nMultiple Conditions\n") print(employees.loc[(employees['Occupation'] != 'Statistician') & (employees['Name'] == 'John')])
C:\python\pandas examples>pycodestyle --first example5.py C:\python\pandas examples>python example5.py Use == operator Age Date Of Join EmpCode Name Occupation 0 23 2018-01-25 Emp001 John Chemist Use < operator Age Date Of Join EmpCode Name Occupation 0 23 2018-01-25 Emp001 John Chemist 1 24 2018-01-26 Emp002 Doe Statistician 3 29 2018-02-26 Emp004 Spark Statistician Use != operator Age Date Of Join EmpCode Name Occupation 0 23 2018-01-25 Emp001 John Chemist 4 40 2018-03-16 Emp005 Mark Programmer Multiple Conditions Age Date Of Join EmpCode Name Occupation 0 23 2018-01-25 Emp001 John Chemist C:\python\pandas examples>
2018-10-17T13:44:58+05:30
2018-10-17T13:44:58+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution