I am trying to create a code that outputs the same results as the pandas' describe().
I want to create a loop inside the dictionary values list to loop over the functions : mean, std, ...
CodePudding user response:
Use pandas to compute your values, then convert to dict:
# example input
# df = pd.DataFrame(np.random.random(size=(5,5)), columns=list('ABCDE'))
# example with count/mean/std, add all the functions you need in the list
df.agg(['count', 'mean', 'std']).to_dict('list')
example:
{'A': [5.0, 0.5033497591814908, 0.25537079639738725],
'B': [5.0, 0.4311195890311792, 0.238291507402266],
'C': [5.0, 0.394948910648723, 0.2937879884789999],
'D': [5.0, 0.5694092003851056, 0.2733118347996942],
'E': [5.0, 0.6275597155186036, 0.17523941542284563]}
or as pointed out by @smci, if you want to customize describe
:
df.select_dtypes('number').describe(percentiles=[0.25,0.5,0.75]).to_dict('list')
CodePudding user response:
The idea is to store the function in an array. Be sure not to call the functions => NO paranthesis.
import pandas as pd
import numpy as np
funcs = [np.mean, np.sum]
df = pd.DataFrame({"col": [4, 5]})
for func in funcs:
print(df["col"].aggregate(func))