Dataframe logical operators

# Dataframe data retrieval using & logical operator.

import pandas as pd

# Create data frame from csv file
df=pd.read_csv("e://data/state-population.csv")


# Data retrieval for state AL in year 2012

print(df[(df['state/region']=='AL') & (df['year']==2012)].head())


Output

state/region ages year population
0 AL under18 2012 1117489.0
1 AL total 2012 4817528.0
# data retrieval state AL and NY (First 5 rows)

df[(df['state/region']=='AL') | (df['state/region']=='NY')].head()

Output

dataframelogical

Published by Python programming examples for beginners

Abhay Gadkari is an IT professional having around experience of 20+ years in IT industry. He worked on web technologies and databases with Insurance and ERP projects.