gpt4 book ai didi

python - 在多索引 pandas DataFrame 中打开 'pretty viewing'

转载 作者:行者123 更新时间:2023-12-01 03:32:59 27 4
gpt4 key购买 nike

我有一个多索引的 pandas 数据框,如下所示:

                                         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 中打开 'pretty viewing',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40678326/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com