为在Pandas中打印的行设置多个限制

时间:2018-04-04 00:06:56

标签: pandas restrictions

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']=='------'])

目前使用df.loc从df列中查找特定值,但会读出包含所有这些值的列,而不仅仅是这些值(如a或vs和statement)

尝试找到一种方法对此设置多个限制,以仅获取符合条件x y和z的列读取。

上面的当前读数:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在指定多个过滤条件时使用&运算符,例如:

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

print(df1)