pandas DataFrame 数据筛选

数值筛选

一、使用【】

1. 单条件筛选

最大逾期天数小于10

due_days=10
last_loan_df=last_loan_df[last_loan_df['max_due_days']<=due_days]

2. 多条件筛选

last_loan_df=last_loan_df[(last_loan_df['max_due_days']<=due_days )|(last_loan_df['score']>100) ]

last_loan_df=last_loan_df[(last_loan_df['max_due_days']<=due_days )&(last_loan_df['score']>100) ]

使用isin方法

# 选择某列等于多个数值或者字符串

last_loan_df[last_loan_df['custid'].isin([1,2,3,4,5])]

字符串的模糊筛选

一. .str.contains()

# 选含有wqbin|bin的行

df.loc[df['name'].str.contains('wqbin|bin']] 


# 选不含wqbin或bin

df.loc[df['name'].str.contains('wqbin|bin'] == False] 

注意:这里只能使用或(|)不能用且(&)

二. .str.startswith()

# 选姓wang的行

df.loc[df['name'].str.startswith('wang']] 

完结!!

其实本质上还是调用了loc

原文地址:https://www.cnblogs.com/wqbin/p/12967326.html