import pandas as pd
import numpy as np
#Create dataframe with numpy array
df = pd.DataFrame(np.random.randint(10,100,size=[5,5]),
columns=["c1","c2","c3","c4","c5"])
print("Original dataframe")
print(df)
#How to use df.apply() in pandas
# In the below code function np.sqrt would be applied to dataframe
print("applying numpy function as paramter in df.apply")
df1 = df.apply(np.sqrt)
print(df1)
#Using lambda function in df.apply(), for squaring the values in dataframe
print("applying lambda function as paramter in df.apply")
my_func = lambda x:x*x
df2 = df.apply(my_func)
print(df2)
'''Output
Original dataframe
c1 c2 c3 c4 c5
0 30 39 62 42 14
1 42 11 87 18 61
2 81 43 16 77 72
3 57 47 94 31 80
4 58 92 96 31 19
applying numpy function as paramter in df.apply
c1 c2 c3 c4 c5
0 5.477226 6.244998 7.874008 6.480741 3.741657
1 6.480741 3.316625 9.327379 4.242641 7.810250
2 9.000000 6.557439 4.000000 8.774964 8.485281
3 7.549834 6.855655 9.695360 5.567764 8.944272
4 7.615773 9.591663 9.797959 5.567764 4.358899
applying lambda function as paramter in df.apply
c1 c2 c3 c4 c5
0 900 1521 3844 1764 196
1 1764 121 7569 324 3721
2 6561 1849 256 5929 5184
3 3249 2209 8836 961 6400
4 3364 8464 9216 961 361
'''