Function Application Continued(apply)...

Apply - Apply a function along an axis(rowwise or columnwise) of the DataFrame.
Example:-
import pandas as pd
import numpy as np
df = pd.DataFrame({'one':pd.Series([1,2,3,4]),'two':pd.Series([3,5,6,7])})
print(df)
print(df.apply(np.mean))
Output:-
one two 0 1 3 1 2 5 2 3 6 3 4 7 one 2.50 two 5.25
Explanation:-
  • df is a dataframe object with some float/number data.
  • df.apply(np.mean) applies mean(average) on the index(columnwise/axis=0) by default
thats why the output above.
  • df.apply(np.mean,axis=1) applies mean on the columns(Rowwise) and gives output as below one    2.50
          two    5.25
Quiz:-
1. import pandas as pd
import numpy as np
df = pd.DataFrame({'one':pd.Series([1,2,3,4]),'two':pd.Series([3,5,6,7])})
print(df.apply(np.sum, axis=0))
#Post your answer in comment

Comments

Popular posts from this blog

Python Tokens

Python Tokens - Operators

Descriptive Statistics - count & sum