Home > Enterprise >  how to put pandas functions in list and run it all?
how to put pandas functions in list and run it all?

Time:07-21

from itertools import cobminations as combi

# this code doesn't work.
for func1, func2 in combi([pd.DataFrame.count,pd.DataFrame.min, 
                           pd.DataFrame.max, pd.DataFrame.std, 
                           pd.DataFrame.var, pd.DataFrame.mean]):
   x = df.col1.func1()
   y = df.col1.func2()
   ax = fig.add_subplot(8, 2, counter)
   ax.scatter(x,y)

this code doesn't work, because the func1&func2 is not the right name to call functions. How Can I loop in pandas functions list? and how can call it?

CodePudding user response:

To answer the direct question, you can pass a dataframe to all the functions you created here, so this code runs:

import pandas as pd
from itertools import combinations

for func1, func2 in combinations([pd.DataFrame.count,pd.DataFrame.min, 
                           pd.DataFrame.max, pd.DataFrame.std, 
                           pd.DataFrame.var, pd.DataFrame.mean], 2):
    x = func1(df).col1
    y = func2(df).col1

But there are a few problems: we're recalculating these things multiple times, and in this method if we have many columns in df, the functions will be applied to those as well. So you could also use the pd.Series version of the functions and pass through the column, like this:

for func1, func2 in combinations([pd.Series.count,pd.Series.min, 
                           pd.Series.max, pd.Series.std, 
                           pd.Series.var, pd.Series.mean], 2):
    x = func1(df.col1)
    y = func2(df.col1)

It's much better to calculate these aggregates with .agg:

aggs = df.col1.agg(['count', 'min', 'max', 'std', 'var', 'mean'])

Now we can use strings access them:

for func1, func2 in combinations(aggs.index, 2):
    x = aggs[func1]
    y = aggs[func2]

CodePudding user response:

Why don't you do a list with the function results? Like, you could do a nested function and apply all the functions you want from numpy/pandas and assign a value. Make a list from those values and assign another list/tuple/dictionary. I recommend dictionaries, they're easier to wok with, specially with Pandas.

Also i think you cannot iterate like the way you're trying to do. Your functions are missing the "()" in the end of the function and that's not how lists work in a for loop.

ps: Your code has "cobminations" instead of combinations. Maybe i've been wrong all along. hehehehehehhe. You didn't import pandas library as well from what you've shown in your code.

  • Related