-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_func.py
55 lines (43 loc) · 1.63 KB
/
apply_func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#-----------------Apply Family of Functions--------------------------------
#To apply our own functions to dataset, pandas provides three functions from
#apply family of functons: pipe(), apply(), applymap()
# pipe():Table wise Function Application.
# It performs the custom operation for the entire dataframe.
import pandas as pd
# own function
def adder(adder1,adder2):return adder1+adder2
#Create a Dictionary of series
d = {'Score_Math':pd.Series([66,57,75,44,31,67,85,33,42,62,51,47]),
'Score_Science':pd.Series([89,87,67,55,47,72,76,79,44,92,93,69])}
print(type(d))
print(d)
df = pd.DataFrame(d)
print (df)
print (df.pipe(adder,2))
# apply():Row or Column Wise Function Application.
# It performs the custom operation for either row wise or column wise.
import numpy as np
#Create a DataFrame
d = {'Score_Math':pd.Series([66,57,75,44,31,67,85,33,42,62,51,47]),
'Score_Science':pd.Series([89,87,67,55,47,72,76,79,44,92,93,69])}
df = pd.DataFrame(d)
print (df)
#Row Wise Fxn Application:
#row wise mean
print (df.apply(np.mean,axis=1))
#Column Wise Fxn Application:
#column wise mean
print (df.apply(np.mean,axis=0))
# applymap():Element wise Function Application.
# applymap():Element wise Function Application.
# It performs specified operation on all the elements of the dataframe.
#Create a DataFrame
d = {'Score_Math':pd.Series([66,57,75,44,31,67,85,33,42,62,51,47]),
'Score_Science':pd.Series([89,87,67,55,47,72,76,79,44,92,93,69])}
df = pd.DataFrame(d)
print (df)
#Example 1:
print (df.applymap(lambda x:x*2))
#Example2:
import math as m
print (df.applymap(lambda x:m.sqrt(x)))