用表格中的值创建仅包含对角线数据的一行 [英] Create one row with values from table with data in diagonal only

查看:121
本文介绍了用表格中的值创建仅包含对角线数据的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫中有下表-

>>>index1   index2   index3   index4   index5   index6   index7
0   sig      null    null     null     null     null     null
1   null      sig     null     null     null     null     null
2   null      null    sig      null     null     null     null
3   null      null    null     sig      null     null     null
4   null      null    null     null     no sig   null     null
5   null      null    null     null     null     no sig   null
6   null      null    null     null     null     null     sig

我想摆脱减少null值的问题,并将数据放在这样的一行中:

I would like to get rid of the reduce the null values and to have the data in one row like this:

>>>index1   index2   index3   index4   index5   index6   index7
0   sig      sig     sig      sig      no sig   no sig   sig

很重要的一点是,如果使用statemnt,我将获得具有空数据的第一个表;我已经为每个索引计算了统计检验,并根据p值,使用append给出了sig或没有sig的每个索引值:

It's important to say that I get the first table with the null data by if statemnt; I have calculated statistic test for each index and based on the p value, I give each index value of sig or no sig, using append:

for i in indices:
    stat, p = friedmanchisquare(df[1][i],df[2][i],df[3][i],df[4][i],df[5][i])
    #print(i,p)
    if p<0.05:
        friedman=friedman.append({i:'sig'},ignore_index=True)
    else:
        friedman=friedman.append({i:'no sig'},ignore_index=True)

因此,我相信附加文件会创建具有许多空值的大表.

So I believe that the append creates the big table with the many null values.

我的最终目标:在循环阶段(例如,使用除append之外的其他东西)或修复"一个单行表.大表之后获得第一行

推荐答案

您可以尝试np.diagonal,它返回diagonal个元素:

You can try np.diagonal which return diagonal elemnts:

d = pd.DataFrame([np.diagonal(df)], columns=df.columns)


  index1 index2 index3 index4  index5  index6 index7
0    sig    sig    sig    sig  no sig  no sig    sig

这篇关于用表格中的值创建仅包含对角线数据的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆