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 call function

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Most people don't understand the knowledge points of this article "how to use pandas call function", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "how to use pandas call function".

0. data preview

The data here is fictional language number outside the score, we demonstrate when copying it.

import pandas as pddf = pd.read_clipboard()df

name

languages

mathematical

English

gender

total score

0

brother Chai

91

95

92

1

1

Xiaoming

82

93

91

1

2

Xiaohua

82

87

94

1

3

grass

96

55

88

0

4

little red

51

41

70

0

5

floret

58

59

40

0

6

Xiaolong

70

55

59

1

7

Jack

53

44

42

1

8

han Meimei

45

51

67

0

1. apply

Apply can perform functions on DataFrame type data by column or row, by default by column (or Series alone).

In case data, for example, we want to replace 1 with male and 0 with female in the gender column, so we can do this.

First define a custom function that takes one parameter s (Series type data).

def getSex(s): if s==1: return 'Male' elif s==0: return 'Female'

The above functions are also written more concisely, which is convenient to understand and uses the most intuitive writing.

Then, we can use apply to call this function directly.

df ['Gender '].apply(getSex)

You can see the output as follows:

0 male

1 man

2 men and

3 women

4 women

5 women

6 men and

7 men and

8 females

Name: gender, dtype: object

Of course, we can also use the form of calling the anonymous function lambda directly:

df ['gender '].apply( lambda s:'male' if s==1 else 'female')

You can see that the result is the same:

0 male

1 man

2 men and

3 women

4 women

5 women

6 men and

7 men and

8 females

Name: gender, dtype: object

The above is simply based on a column of value conditions for data processing, we can also be based on multi-column combination conditions (can be understood as row) for processing, it is necessary to note that in this case you need to specify the parameter axis=1, see the following example.

In this case, we consider a total score above 200 and a math score above 90 to be a high score.

#Multi-column conditional combination df ('level ')= df.apply(lambda df:'high score' if df ('total score')>=200 and df ('math ')>=90 else ' other', axis=1)df

Similarly, the above functions called with apply are custom, in fact we can also call built-in or pandas/numpy and other built-in functions.

For example, find the number of words and the highest total score:

# python built-in function df["'Chinese ',' Math','English',' Total Score']].apply(max)

Languages 96

Mathematics 95

English 94

Total score 278

dtype: int64

Find the number of words and the average score:

# numpy's built-in function import numpy as npdf[['Chinese',' Math','English',' Total Score']].apply(np.mean)

Language 69.77778

Math 64.44444

English 71.44444

Total score 205.66667

dtype: float64

2. applymap

Applymap is a function of each element, and variables are the values of each element.

For example, more than 90 points in three subjects other than the number of languages are considered to be high scores in subjects

df[['Chinese',' Math','English']].applymap(lambda x:' high score ' if x>=90 else ' other')

languages

mathematical

English

0

high score

high score

1

other

high score

2

other

other

3

high score

other

4

other

other

5

other

other

6

other

other

7

other

other

8

other

other

3. map

Map is to return the final data according to the input mapping value, acting on a certain column. The value passed in can be a dictionary, the key value can be the original value, and the value can be the value that needs to be replaced. You can also pass in a function or character formatting expression, etc.

For example, replace 1 with male and 0 with female in the gender column above. It can also be realized by map.

df ['Gender '].map({1:' Male', 0:'Female'})

The output is also consistent:

0 male

1 man

2 men and

3 women

4 women

5 women

6 men and

7 men and

8 females

Name: gender, dtype: object

For example, the total column wants to be formatted characters:

df ['Total Score'].map ('Total Score: {} Score'. format)

0 Total score: 278 points

Total score: 266 points

Total score: 263 points

Total score: 239 points

Total score: 162 points

5 Total score: 157 points

Total score: 184 points

7 Total score: 139 points

8 Total score: 163 points

Name: Total, dtype: object

4. agg

Agg is generally used for aggregation, often seen in grouping or perspective operations, and its usage is closer to apply.

For example, find the highest, lowest and average scores of the total score and the number of words

df[['Chinese',' Math','English',' Total Score']].agg(['max',' min','mean'])

We can also perform different operations on different columns (specified in dictionary form)

#Highest score for Chinese, lowest score for Math and highest score for English df.agg({'Chinese':[' max'],'Math':' min','English':[' max','min']})

Of course, custom function calls are also supported.

5. pipe

For the above four methods that call functions, we find that the parameters of the called function are DataFrame or Serise data. If we need other parameters for the called function, what should we do?

Then the pipe appeared.

Pipe, also known as pipeline method, can standardize and streamline our processing and analysis process. It can take other parameters of the called function when calling the function, so it is convenient to extend the function of the custom function.

For example, we need to get the data of students with a total score greater than n and sex, where n and sex are variable parameters, so it is not easy to use apply and so on. At this time, you can use the pipe method to make trouble!

Let's start by defining a function:

#Define a function with a total score greater than or equal to n and sex as classmate data (sex = 2 means regardless of gender) def total(df, n, sex):dfT = df.copy()if sex == 2:return dfT[(dfT <$'total score']>=n)]else:return dfT[(dfT <$'total score']>=n) & (dfT <$'gender ']==sex)]

If we want to find students with scores greater than 200, regardless of gender, we can do this:

df.pipe(total,200,2)

If you look for a student with a total score of more than 150 and a gender of male (1), you can do this:

df.pipe(total,150,1)

If you look for a student with a total score of more than 200 and a gender of female (0), you can do this:

df.pipe(total,200,0)

The above is the content of this article about "how to use pandas call function". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge content, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report