Create vectors for H-test Kruskal-Wallis

I have a dataset below

df = pd.DataFrame({'numbers':range(9), 'group':['a', 'b', 'c']*3})

 group numbers
0   a   0
1   b   1
2   c   2
3   a   3
4   b   4
5   c   5
6   a   6
7   b   7
8   c   8

I want to create vectors

a = [0, 3, 6]
b = [1, 4, 7]
c = [2, 5, 8]

for pythons H-test Kruskal-Wallis

stats.kruskal(a, b, c)

or maybe analog, as in R (numbers ~ group)

0
source share
1 answer

I am not familiar with any special requirements of the Kruskal-Wallis test, but you can access these grouped arrays by putting them into the dictionary as follows:

groupednumbers = {}
for grp in df['group'].unique(): 
    groupednumbers[grp] = df['numbers'][df['group']==grp].values

print(groupednumbers)
*** {'c': array([2, 5, 8]), 'b': array([1, 4, 7]), 'a': array([0, 3, 6])}

That is, you get your vectors, explicitly calling groupednumbers['a'], etc., or through a list:

args = groupednumbers.values()

... or if you need them in order:

args = [groupednumbers[grp] for grp in sorted(df['group'].unique())]

And then call

stats.kruskal(*args)

Or, if you need actual listings, you can do that list(df['numbers'][...].values.)

+2
source

Source: https://habr.com/ru/post/1589209/


All Articles