python - 如何在 Pandas 中对多个索引下的列重新排序

标签 python pandas

我有以下数据:

Set Adjuvant    Route   Organ   Up  Down
set7    FOO ID  LV  11  14
set7    BAR ID  LV  17  15
set7    QUX ID  LV  10  22
set7    FOO ID  SP  14  13
set7    BAR ID  SP  13  8
set7    QUX ID  SP  11  18
set7    FOO ID  LN  25  25
set7    BAR ID  LN  38  16
set7    QUX ID  LN  53  18

我已阅读并使用以下代码重新组织它:

import pandas as pd 
df = pd.io.parsers.read_table("http://dpaste.com/3QB2YQX.txt")
df = df.pivot(index="Adjuvant",columns="Organ")

# Remove unwanted index name
df.drop('Set',axis=1,inplace=True)
df.drop('Route',axis=1,inplace=True)
df.index.name = None
df.columns.names = (None,None)

ndf = df.reindex(sorted(df.index, key=lambda x: x.lower()))

这使得这个数据框:

In [35]: ndf
Out[35]:
     Up         Down
     LN  LV  SP   LN  LV  SP
BAR  38  17  13   16  15   8
FOO  25  11  14   25  14  13
QUX  53  10  11   18  22  18

我想要做的是将 UP/DOWN 索引下的列从 LN, LV, SP 渲染到 LV, SP, LN >。我该怎么做?

最佳答案

使用set_levels :

In [3]: ndf[ndf.columns.set_levels(['LV', 'SP','LN'], level=1)]
Out[3]:
     Up         Down
     LV  SP  LN   LV  SP  LN
BAR  17  13  38   15   8  16
FOO  11  14  25   14  13  25
QUX  10  11  53   22  18  18

这在网站上没有记录,没有专门的页面,但如果您执行选项卡查找来显示文档字符串,它会显示以下内容:

Signature: ndf.columns.set_levels(levels, level=None, inplace=False, verify_integrity=True)
Docstring:
Set new levels on MultiIndex. Defaults to returning
new index.

Parameters
----------
levels : sequence or list of sequence
    new level(s) to apply
level : int or level name, or sequence of int / level names (default None)
    level(s) to set (None for all levels)
inplace : bool
    if True, mutates in place
verify_integrity : bool (default True)
    if True, checks that levels and labels are compatible

Returns
-------
new index (of same type and class...etc)


Examples
--------
>>> idx = MultiIndex.from_tuples([(1, u'one'), (1, u'two'),
                                  (2, u'one'), (2, u'two')],
                                  names=['foo', 'bar'])
>>> idx.set_levels([['a','b'], [1,2]])
MultiIndex(levels=[[u'a', u'b'], [1, 2]],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_levels(['a','b'], level=0)
MultiIndex(levels=[[u'a', u'b'], [u'one', u'two']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_levels(['a','b'], level='bar')
MultiIndex(levels=[[1, 2], [u'a', u'b']],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=[u'foo', u'bar'])
>>> idx.set_levels([['a','b'], [1,2]], level=[0,1])
MultiIndex(levels=[[u'a', u'b'], [1, 2]],
           labels=[[0, 0, 1, 1], [0, 1, 0, 1]],
           names=[u'foo', u'bar'])
File:      c:\winpython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\pandas\core\index.py
Type:      method

关于python - 如何在 Pandas 中对多个索引下的列重新排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34283267/

相关文章:

python - 如何从 python 代码构建 pyinstaller 规范文件

python - 我的输出需要是这样的,它没有出现我需要的样子

Python:使用正则表达式或其他方式提取 URL

python - 如何从语料库中删除无意义或不完整的单词?

python - 如何使用 Python 获取电子邮件文本内容?

python - 时间序列 Pandas 的滚动平均值

python - 如何从DataFrame中的名字中提取首字母

python - pandas.read_csv : OperationalError: table has no column named 1

python - 基于 pandas 一定范围的随机日期

python - 雅虎 HTML 抓取工具! Finance 使用 lxml 和 requests 返回错误值