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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM