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

                                         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'])

关于python - 在多索引 Pandas DataFrame中打开“漂亮查看”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40678326/

10-12 16:38