简体   繁体   English

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

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

I have a multi-indexed pandas dataframe which looks like this: 我有一个多索引的熊猫数据框,看起来像这样:

                                         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

I've seen multi indexed data frames in the doc that are 'sparce-ified' to pretty viewing. 我已经在文档中看到了多索引的数据帧,这些数据帧“稀疏化”了,可以漂亮地查看。 So in this case it would look something like this: 因此,在这种情况下,它将看起来像这样:

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

Does anybody know how to turn this option on? 有人知道如何打开此选项吗? I've tried pandas.set_option('display.multi_sparse', True) but this did not work. 我已经尝试过pandas.set_option('display.multi_sparse', True)但这没有用。

=============Edit ================================== ===============================================

To create the multi-index I used: 要创建我使用的多索引:

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

When I use df.index I get the following output: 当我使用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)

You can use MultiIndex.from_tuples , because index contains tuples : 您可以使用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

EDIT: 编辑:

It seems you need set_index : 看来您需要set_index

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

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

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