Function Application Continued(transform)...

Transform - This function is used to change the data elements, lets say some data are not how you wannt them to be to process it, usually used during the data preparation step in machine learning.
Example1:-
import pandas as pd
import numpy as np
df = pd.DataFrame({'A':range(3),'B':range(1,4)})
print(df.transform(lambda x:x+1))
Output:-
A B
0 1 2
1 2 3
2 3 4
Explanation:-
  • range(3) gives a series of number from 0 to 2
  • range(1,4) gives a series of number from 1 to 3
  • lambda operator takes each element x and adds 1 to it.
Example2:-
import pandas as pd
import numpy as np
s = pd.Series(range(3))
print(s.transform([np.sqrt,np.exp]))
Output:-
sqrt exp
0 0.000 1.000
1 1.000 2.718
2 1.414 7.389
Explanation:-
  • range(3) gives a series of number from 0 to 2
  • s.transform function applies two operation(square root and exponential) on each element of series s.

Comments

Popular posts from this blog

Python Tokens

Python Tokens - Operators

Descriptive Statistics - count & sum