Home > Software engineering >  How to pass a list as multiple variables in scipy?
How to pass a list as multiple variables in scipy?

Time:12-23

I am new to python, and I can't find a solution to my problem since I am not even really sure what I am looking for or what I should search for in google.

I have a dataframe with 30 columns and I want to do a Friedman test with scipy.stats.friedmanchisquare

As an argument I need all the samples which are my df's columns.

I tried various methods but I can't figure it out:

scipy.stats.friedmanchisquare(df)
lst_cols = ['col1', 'col2', 'col3',..., 'col30']
scipy.stats.friedmanchisquare(df[lst_cols])
lst_cols = ['col1', 'col2', 'col3',..., 'col30']
samples = []
for i in lst_cols:
    lst = df[i].tolist()
    samples.append(lst)

scipy.stats.friedmanchisquare(samples)

But I always get the Error: At least 3 sets of samples must be given for Friedman test, got 1

I get that that I seem to always pass a single list / single df. How can I use the elements of a list as seperate samples?

greetings

CodePudding user response:

Try to use * before list, it expands list to values

lst_cols = ['col1', 'col2', 'col3',..., 'col30']
samples = []
for i in lst_cols:
    lst = df[i].tolist()
    samples.append(lst)

scipy.stats.friedmanchisquare(*samples)

or even simple

scipy.stats.friedmanchisquare(*df.values)

CodePudding user response:

When calling a function that takes multiple arguments, you can use a list (or any sequence really) as separate arguments with the * operator:

def foo(a, b):
    return a b
    
l = [1, 2]

# unpacks l i to a and b
foo(*l)

So just stick a * in front of your columns:

scipy.stats.friedmanchisquare(*df[lst_cols])
  • Related