Apply Function On Each Column In A Pandas Dataframe
How I can write following function in more pandas way: def calculate_df_columns_mean(self, df): means = {} for column in df.columns.columns.tolist():
Solution 1:
It seems to me that the iteration over the columns is unnecessary:
defcalculate_df_columns_mean(self, df):
cleaned_data = self.remove_outliers(df[column].tolist())
return cleaned_data.mean()
the above should be enough assuming that remove_outliers
still returns a df
EDIT
I think the following should work:
defcalculate_df_columns_mean(self, df):
return df.apply(lambdax: remove_outliers(x.tolist()).mean()
Solution 2:
Use dataFrame.apply(func, axis=0)
:
# axis=0 means apply to columns; axis=1 to rows
df.apply(numpy.sum, axis=0) # equiv to df.sum(0)
Post a Comment for "Apply Function On Each Column In A Pandas Dataframe"