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 toimport
pandas as pd
data
=
pd.read_excel(r
' Sales data .xlsx'
)
print
(data)
The data are as follows :
“ 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)]
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])]
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 !