pandasrestrictions

Set Multiple Restrictions for Rows Called to Print in Pandas


import pandas as pd
import numpy as np

#load data
#data file and py file must be in same file path
df = pd.read_csv('cbp15st.txt', delimiter = ',', encoding = 'utf-8- 
sig')

#define load data DataFrame columns

state = df['FIPSTATE']

industry = df['NAICS']

legal_form_of_organization = df['LFO']

suppression_flag = df['EMPFLAG']

total_establishment = df['EST']

establishment_1_4 = df['N1_4']

establishment_5_9 = df['N5_9']

establishment_10_19 = df['N10_19']

establishment_20_49 = df['N20_49']

establishment_50_99 = df['N50_99']

establishment_100_249 = df['N100_249']

establishment_250_499 = df['N250_499']

establishment_500_999 = df['N500_999']

establishment_1000_more = df['N1000']

#use df.loc to parse dataset for partiuclar value types

print(df.loc[df['EMPFLAG']=='A'], df.loc[df['FIPSTATE']==1], 
df.loc[df['NAICS']=='------'])

Currently using df.loc to locate specific values from the df columns, but will read out those columns that contain all of these values, not only these values (like an or vs and statement)

Trying to find a way to place multiple restrictions on this to only get column reads that meet criteria x y and z.

Current Readout from above:

enter image description here


Solution

  • You can use & operator while specifying multiple filtering criteria, something like:

    df1 = df.loc[(df['EMPFLAG']=='A']) & (df['FIPSTATE']==1) & (df['NAICS']=='------')]
    
    print(df1)