Pandas custom function

python

#1

Can anyone help me out in writing pandas custom functions? Every-time i fall in the confusion loop in accessing and deriving specific row and col values as custom results, i want to execute col means without using slicing no user defined functions like numpy(np.mean) and i need only parameter to pass is dataset ‘df’ to custom function

Below is my dataset please give me code logic in getting col means.

df = pd.DataFrame({"A": [10,20,30], "B": [20, 30, 10]})
def col_men(df):
means=[0 for i in range(df.shape[1])]
for k in range(df.shape[1]):
    col_values=[row[k] for row in df]
    means[k]=sum(col_values)/float(len(df))
return means

#2

Hi,

In order to get mean for each column, you can use

 sum=0
 n=len(df)
for i in range (0,n) #range will be the number of rows you have
      sum=df['A'][i]
mean = sum/n

#3

thank you