对于以下df
data=[['TAMU', 54, 0, 0, 6, 5, 0,],['UIUC', 33, 43, 5, 0, 76, 81],
['USC',4, 1, 0, 7, 21, 4], ['Austin',22,31, 0, 0,55, 0],
['UCLA', 55, 6, 7, 9, 11,12]]
pd.DataFrame(data,columns = ['Name', 'Research', 'Thesis',
'Proposal', 'AI', 'Analytics', 'Data'])
我想为两个指定行(比如USC和UCLA)的所有可能的列组合(例如:AI,Analytics--Data,AI)创建列联表,以提供给我的chisquare函数
def overflow(school1,school2,alpha):
pvals_list=[]
data=[['TAMU', 54, 0, 0, 6, 5, 0,],['UIUC', 33, 43, 5, 0, 76, 81],
['USC',4, 1, 0, 7, 21, 4], ['Austin',22,31, 0, 0,55, 0],
['UCLA', 55, 6, 7, 9, 11,12]]
pd.DataFrame(data,columns = ['Name', 'Research', 'Thesis', 'Proposal',
'AI', 'Analytics', 'Data'])
df=df[(df['Unnamed: 0'] == school1) | (df['Unnamed: 0'] == school2)]
df=df.loc[:, df.ne(0).all()]
df=df.set_index('Name')
###
####code to create columns pairs [for loop?]to feed to data_crosstab below
###
data_crosstab = pd.crosstab()
chi,p_vals = stats.chi2_contingency(data_crosstab)[:2]
if p > alpha:
pvals_list.appned(p_vals)
return(pvals_list)
overflow('USC','UCLA',0.05)
编辑:到目前为止,我已经尝试了几种不同的方法,但没有一种有效。我们将非常感谢您的帮助。
这是你想要的吗
输出:
您需要将这两个数据传递到
pd.crosstab
以创建RxC Table
:然后您可以将其传递给
scipy.stats.chi2_contingency
以获得结果:对于上面的一对行索引,可以正常工作,只需替换
USC
和UCLA
如果要对所有行执行此操作,可以在索引值上使用
itertools
中的combinations
进行循环:IIUC,你想要
itertools.combinations
:使用
combinations
的结果是:相关问题 更多 >
编程相关推荐