Function Application CBSE IP 12

To apply any built in library functions such as mean, sum etc. to the pandas object(dataframe) or your own functions their are different types of functions
Pipe - applies function to the dataframe objects each element.

Example:-
import pandas as pd
import numpy as np
def func1(ele1,ele2):
return ele1+ele2
df = pd.DataFrame(np.random.randn(5,3),columns=['col1','col2','col3'])
print(df)
print(df.pipe(func1,10))
Output:-
col1 col2 col3 0 0.994857 -0.137329 0.992666 1 0.115403 -1.273267 -0.030973 2 -0.865839 -0.575995 1.629540 3 -0.649246 1.127552 0.750935 4 -0.369847 0.100534 -0.214271 col1 col2 col3 0 10.994857 9.862671 10.992666 1 10.115403 8.726733 9.969027 2 9.134161 9.424005 11.629540 3 9.350754 11.127552 10.750935 4 9.630153 10.100534 9.785729

Explanation:-

  • random.randn(5,3) is a library function found inside numpy module, it creates 5x3 i.e 5 rows and 3 columns dataframe object.
  • def func1(ele1,ele2) is a user defined function that we have created to add value of 10 to each element in dataframe. User defined function can have zero or more argument accor. It takes 2 argument, first is ele1 i.e the object to be modified in our case each
element of "df" and ele2 i.e how to modify in our case 10.
  • As you can see the first output of print('df') has all random numbers ranging from -0 to 1
  • After function call to df.pipe(add,10) which takes 2 arguments first is "func1" name of function and second is value 10 to add to each element which is the second argument of "func1" function.
Quiz:-
1. import pandas as pd
import numpy as np
def func1(ele1):
return "Hello " + ele1
def func2(ele1):
return ele1 + " Again"
df = pd.DataFrame({'col1':pd.Series(['aaa','bbb']),'col2':pd.Series(['ccc','ddd'])})
print(df.pipe(func1).pipe(func2))
#Post your answer in the comment below
2. import pandas as pd
import numpy as np
df = pd.DataFrame([[1,2,3],[4,5,6]],columns=['a','b','c'])
print(df.pipe(np.mean))
#Post your answer in the comment below
3. import pandas as pd
import numpy as np
df = pd.DataFrame([[1,2,3],[4,5,6]],columns=['a','b','c'])
print(df.pipe(np.mean,axis=1))
#Post your answer in the comment below

Comments

Post a Comment

Popular posts from this blog

Python Tokens

Python Tokens - Operators

Descriptive Statistics - count & sum