繁体   English   中英

在多索引熊猫DataFrame中打开“漂亮查看”

[英]Turning on 'pretty viewing' in multi-indexed pandas DataFrame

我有一个多索引的熊猫数据框,看起来像这样:

                                         RFI
(Smad3_pS423/425_customer, 0, 1)    0.664263
(Smad3_pS423/425_customer, 0, 2)    0.209911
(Smad3_pS423/425_customer, 0, 3)    0.099809
(Smad3_pS423/425_customer, 5, 1)    0.059652

我已经在文档中看到了多索引的数据帧,这些数据帧“稀疏化”了,可以漂亮地查看。 因此,在这种情况下,它将看起来像这样:

                                         RFI
Smad3_pS423/425_customer  0  1    0.664263
                             2    0.209911
                             3    0.099809
                          5  1    0.059652

有人知道如何打开此选项吗? 我已经尝试过pandas.set_option('display.multi_sparse', True)但这没有用。

===============================================

要创建我使用的多索引:

    df.index=df[['Antibody','Time','Repeats']]
    df.drop(['Antibody','Time','Repeats'],axis=1,inplace=True)

当我使用df.index时,得到以下输出:

Index([ (u'Smad3_pS423/425_customer', u'0', u'1'),
        (u'Smad3_pS423/425_customer', u'0', u'2'),
        (u'Smad3_pS423/425_customer', u'0', u'3'),
        (u'Smad3_pS423/425_customer', u'5', u'1'),
        (u'Smad3_pS423/425_customer', u'5', u'2'),
        (u'Smad3_pS423/425_customer', u'5', u'3'),
       (u'Smad3_pS423/425_customer', u'10', u'1'),
       (u'Smad3_pS423/425_customer', u'10', u'2'),
       (u'Smad3_pS423/425_customer', u'10', u'3'),
       (u'Smad3_pS423/425_customer', u'20', u'1'),
       ...
                     (u'a-Tubulin', u'120', u'3'),
                     (u'a-Tubulin', u'180', u'1'),
                     (u'a-Tubulin', u'180', u'2'),
                     (u'a-Tubulin', u'180', u'3'),
                     (u'a-Tubulin', u'240', u'1'),
                     (u'a-Tubulin', u'240', u'2'),
                     (u'a-Tubulin', u'240', u'3'),
                     (u'a-Tubulin', u'300', u'1'),
                     (u'a-Tubulin', u'300', u'2'),
                     (u'a-Tubulin', u'300', u'3')],
      dtype='object', length=216)

您可以使用MultiIndex.from_tuples ,因为index包含tuples

df = pd.DataFrame({'RFI':[0.664263, 0.209911, 0.099809, 0.059652]}, 
                   index=[('Smad3_pS423/425_customer', 0, 1),
                          ('Smad3_pS423/425_customer', 0, 2),
                          ('Smad3_pS423/425_customer', 0, 3),
                          ('Smad3_pS423/425_customer', 5, 1) ])
print (df)
                                       RFI
(Smad3_pS423/425_customer, 0, 1)  0.664263
(Smad3_pS423/425_customer, 0, 2)  0.209911
(Smad3_pS423/425_customer, 0, 3)  0.099809
(Smad3_pS423/425_customer, 5, 1)  0.059652
df.index = pd.MultiIndex.from_tuples(df.index)
print (df)
                                   RFI
Smad3_pS423/425_customer 0 1  0.664263
                           2  0.209911
                           3  0.099809
                         5 1  0.059652

编辑:

看来您需要set_index

df = df.set_index(['Antibody','Time','Repeats'])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM