create vectors for Kruskal-Wallis H-test python

与世无争的帅哥 提交于 2020-01-06 03:03:26

问题


I have dataset as 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 Kruskal-Wallis H-test python

stats.kruskal(a, b, c)

or maybe analogue as in R (numbers ~ group)


回答1:


I'm not familiar with any special requirements of the Kruskal-Wallis test, but you can access these grouped arrays via by putting them into a dictionary this way:

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'd get your vectors by either explicitly calling groupednumbers['a'] etc., or via a list:

args = groupednumbers.values()

... or if you need them in an order:

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

And then call

stats.kruskal(*args)

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



来源:https://stackoverflow.com/questions/35816865/create-vectors-for-kruskal-wallis-h-test-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!