Coursera Learner working on a presentation with Coursera logo and
Coursera Learner working on a presentation with Coursera logo and

Pandas.apply enable the clients to pass a capacity and apply it on each and every estimation of the Pandas arrangement. It comes as a colossal improvement for the pandas library as this capacity isolates information as indicated by the conditions required because of which it is productively utilized in information science and AI. 

Establishment: 

pip install pandas

Import the Pandas module into the python record utilizing the accompanying directions on the terminal:

import pandas as pd

s = pd.read_csv(“stock.csv”, squeeze=True)

Syntax:

s.apply(func, convert_dtype=True, args=())

Parameters:

func: .apply takes a function and applies it to all values of pandas series.
convert_dtype: Convert dtype as per the function’s operation.
args=(): Additional arguments to pass to function instead of series.
Return Type: Pandas Series after applied function/operation.

Example #1:

The following example passes a function and checks the value of each element in series and returns low, normal or High accordingly.

import pandas as pd 

# reading csv 

s = pd.read_csv(“stock.csv”, squeeze = True) 

# defining function to check price 

def fun(num): 

    if num<200: 

        return “Low”

    elif num>= 200 and num<400: 

        return “Normal”

    else: 

        return “High”

# passing function to apply and storing returned series in new 

new = s.apply(fun) 

# printing first 3 element 

print(new.head(3)) 

# printing elements somewhere near the middle of series 

print(new[1400], new[1500], new[1600]) 

# printing last 3 elements 

print(new.tail(3)) 

https://media.geeksforgeeks.org/wp-content/cdn-uploads/apply_pandas.jpg

Output

Example 2

In the following example, a temporary anonymous function is made in .apply itself using lambda. It adds 5 to each value in series and returns a new series.

import pandas as pd 

s = pd.read_csv(“stock.csv”, squeeze = True) 

# adding 5 to each value 

new = s.apply(lambda num : num + 5) 

# printing first 5 elements of old and new series 

print(s.head(), ‘\n’, new.head()) 

# printing last 5 elements of old and new series 

print(‘\n\n’, s.tail(), ‘\n’, new.tail()) 

Output:

0    50.12

1    54.10

2    54.65

3    52.38

4    52.95

Name: Stock Price, dtype: float64 

0    55.12

1    59.10

2    59.65

3    57.38

4    57.95

Name: Stock Price, dtype: float64

3007    772.88

3008    771.07

3009    773.18

3010    771.61

3011    782.22

Name: Stock Price, dtype: float64

3007    777.88

3008    776.07

3009    778.18

3010    776.61

3011    787.22

Name: Stock Price, dtype: float64

Languages

Weekly newsletter

No spam. Just the latest releases and tips, interesting articles, and exclusive interviews in your inbox every week.