程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python pandas conditional filtering function

編輯:Python

source :https://www.jb51.net/article/239880.htm 

This article mainly introduces Python Pandas Condition filtering function , Filtering is a function that is frequently used in daily work , The following is a detailed introduction to relevant information , Please refer to
Catalog
  • One 、 Prepare the data
  • Two 、 With >,<,==,>=,<= To make a choice
  • 3、 ... and 、.isin()
  • Four 、.str.contains() Realization

One 、 Prepare the data

1 2 3 4 importpandas as pd   data =pd.read_excel(r' Sales data .xlsx')print(data)

The data are as follows :

Two 、 With >,<,==,>=,<= To make a choice

“ be equal to ” It must be with ‘==’, If you use ‘=’ It's not about size :

for example : Screening the data of the salesperson is sister ma

df = data[data[' Salesperson '] == ' Sister Ma ']

for example : Select the data that the salesperson is sister Ma, and the sales volume of Tianhe store is greater than 100 The data of

        Use &( And ) and |( or ) Each condition should be enclosed in parentheses

df = data[(data[' Salesperson '] == ' Sister Ma ') & (data[' Tianhe store sales '] > 100)]

3、 ... and 、.isin()

If you want to select a column equal to multiple values or strings , Want to use .isin(), We put df Modified it (isin() There should be a list):

for example : Screening Tianhe store sales is equal to 180 and 200 The data of

df = data[data[' Tianhe store sales '].isin([180, 200])]

Four 、.str.contains() Realization

The most commonly used filter should be the fuzzy filter of string , stay SQL What is used in the sentence is like, stay pandas We can use it in the .str.contains() To achieve .

for example : Filter the data of salesperson with horse word

df = data[data[' Salesperson '].str.contains(' Horse ')]

You can also use '|' To filter multiple conditions

  for example : Filter the data of salesperson with horse word or Li word

df = data[data[' Salesperson '].str.contains(' Horse | Li ')]

Be careful : This ‘|’ It's in quotation marks , Instead of two words

  This is about Python Pandas This is the end of the article on conditional filtering , More about Pandas For conditional filtering content, please search the previous articles of script home or continue to browse the relevant articles below. I hope you will support script home in the future !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved