I'm doing a bunch of operations on pandas dataframes. For example finding max, min and average inside columns and return the column names in a new column. Now I'm trying to wrap these things into a function, and use max() and/or min() as arguments in this function.
Below is a snippet that describes what I'm trying to do in a very simplified way. In its current state it also returns a description of the desired output. The snippet does not have the desired functionality and flexibility though.
The setup:
# Sample dataframe df = pd.DataFrame({'col_A':[1,20,6,1,3]}) def findValue(function, df, colname): print(function) # just a placeholder df[colname] = df.max()[0] return df df2 = findValue(function='max', df=df, colname='col_B') print(df) Output 1:
col_A col_B 0 1 20 1 20 20 2 6 20 3 1 20 4 3 20 A naive attempt:
# Sample dataframe df = pd.DataFrame({'col_A':[1,20,6,1,3]}) # The function I would like to use in another function is max() # My function def findValue(function, df, colname): df[colname] = df.function()[0] return df df2 = findValue(function=max(), df=df , colname='col_B') print(df) Output 2:
Traceback (most recent call last): File "<ipython-input-7-85964ff29e69>", line 1, in <module> df2 = findValue(function=max(), df=df , colname='col_B') TypeError: max expected 1 arguments, got 0 How can I change the above snippet so that I can change function = max() to function = min() or any other function in the arguments of findValue()? Or even define a list of functions to be used in a similar manner?
Thank you for any suggestions!
describe()method? Will return a DataFrame of max, min, mean, std. dev., count for each col in the DataFrame (or a Series of these values, if run on a Series). [docs] (pandas.pydata.org/pandas-docs/stable/generated/…)describe(). But it will certainly be useful, so thank you for the tip!