Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use pandas to realize conditional selection in Python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

It is believed that many inexperienced people are at a loss about how to use pandas to choose according to conditions in Python. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Numpy.where method

Excel function has a function that beginners can learn right away-IF function, but there is no corresponding method in pandas, because numpy already has a corresponding implementation-where.

It can return different values based on the condition (true or false). Because you need to use the numpy method, you need to import the numpy package at the beginning of the code:

1import pandas as pd

2import numpy as np

Scene

The student transcript is as follows:

A score higher than or equal to 60 points is qualified, and column C is marked with "yes", otherwise with "no".

A typical requirement for selecting a value according to conditions

How to solve the problem?

Such a simple requirement can be easily solved by an IF function in Excel:

The first parameter of the IF function is the condition, the second parameter is the return when the first condition is true, and the third parameter is the return when the first condition is false.

The logic when using the numpy.where method is the same as the IF function of Excel above:

1df = pd.read_excel ('data.xlsx',' sp1')

2df ['res'] = np.where (df. Score > = 60 'Yes','No')

3df

Each parameter of the row 2:np.where can accept the column of pandas (Series)

Superior performance

If you have read this series of articles, you will find that when you first started Python, you learned all kinds of skills to deal with lists and dictionaries. Even if and for loops are rarely used.

In fact, you can choose to use the basic syntax of Python in pandas.

For example, in the above example, we can use apply:

1df = pd.read_excel ('data.xlsx',' sp1')

two

3def ap_where (x):

4 if x > = 60:

5 return 'Yes'

6 return'No'

seven

8df ['res'] = df. Grade. Apply (ap_where)

9df

After reading the above, have you mastered how to use pandas to achieve conditional selection in Python? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report