- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一些代码总结了一个包含著名的泰坦尼克号数据集的 DataFrame,如下所示:
titanic['agecat'] = pd.cut(titanic.age, [0, 13, 20, 64, 100],
labels=['child', 'adolescent', 'adult', 'senior'])
titanic.groupby(['agecat', 'pclass','sex']
)['survived'].mean()
这会根据 groupby
调用生成以下带有 MultiIndex 的 DataFrame:
agecat pclass sex
adolescent 1 female 1.000000
male 0.200000
2 female 0.923077
male 0.117647
3 female 0.542857
male 0.125000
adult 1 female 0.965517
male 0.343284
2 female 0.868421
male 0.078125
3 female 0.441860
male 0.159184
child 1 female 0.000000
male 1.000000
2 female 1.000000
male 1.000000
3 female 0.483871
male 0.324324
senior 1 female 1.000000
male 0.142857
2 male 0.000000
3 male 0.000000
Name: survived, dtype: float64
但是,我希望 MultiIndex 的 agecat
级别自然排序,而不是按字母顺序排序,即:['child', 'adolescent', 'adult', '高级']
。但是,如果我尝试使用 reindex
来执行此操作:
titanic.groupby(['agecat', 'pclass','sex'])['survived'].mean().reindex(
['child', 'adolescent', 'adult', 'senior'], level='agecat')
它对结果 DataFrame 的 MultiIndex 没有任何影响。这应该有效,还是我使用了错误的方法?
最佳答案
你需要提供一个重新排序的MultiIndex
In [36]: index = MultiIndex(levels=[['foo', 'bar', 'baz', 'qux'],
['one', 'two', 'three']],
labels=[[0, 0, 0, 1, 1, 2, 2, 3, 3, 3],
[0, 1, 2, 0, 1, 1, 2, 0, 1, 2]],
names=['first', 'second'])
In [37]: df = DataFrame(np.random.randn(10, 3), index=index,
columns=Index(['A', 'B', 'C'], name='exp'))
In [38]: df
Out[38]:
exp A B C
first second
foo one -1.007742 2.594146 1.211697
two 1.280218 0.799940 0.039380
three -0.501615 -0.136437 0.997753
bar one -0.201222 0.060552 0.480552
two -0.758227 0.457597 -0.648014
baz two -0.326620 1.046366 -2.047380
three 0.395894 1.128850 -1.126649
qux one -0.353886 -1.200079 0.493888
two -0.124532 0.114733 1.991793
three -1.042094 1.079344 -0.153037
通过在第二层进行排序来模拟重新排序
In [39]: idx = df.sortlevel(level='second').index
In [40]: idx
Out[40]:
MultiIndex
[(u'foo', u'one'), (u'bar', u'one'), (u'qux', u'one'), (u'foo', u'two'), (u'bar', u'two'), (u'baz', u'two'), (u'qux', u'two'), (u'foo', u'three'), (u'baz', u'three'), (u'qux', u'three')]
In [41]: df.reindex(idx)
Out[41]:
exp A B C
first second
foo one -1.007742 2.594146 1.211697
bar one -0.201222 0.060552 0.480552
qux one -0.353886 -1.200079 0.493888
foo two 1.280218 0.799940 0.039380
bar two -0.758227 0.457597 -0.648014
baz two -0.326620 1.046366 -2.047380
qux two -0.124532 0.114733 1.991793
foo three -0.501615 -0.136437 0.997753
baz three 0.395894 1.128850 -1.126649
qux three -1.042094 1.079344 -0.153037
不同的顺序
In [42]: idx = idx[5:] + idx[:5]
In [43]: idx
Out[43]:
MultiIndex
[(u'bar', u'one'), (u'bar', u'two'), (u'baz', u'three'), (u'baz', u'two'), (u'foo', u'one'), (u'foo', u'three'), (u'foo', u'two'), (u'qux', u'one'), (u'qux', u'three'), (u'qux', u'two')]
In [44]: df.reindex(idx)
Out[44]:
exp A B C
first second
bar one -0.201222 0.060552 0.480552
two -0.758227 0.457597 -0.648014
baz three 0.395894 1.128850 -1.126649
two -0.326620 1.046366 -2.047380
foo one -1.007742 2.594146 1.211697
three -0.501615 -0.136437 0.997753
two 1.280218 0.799940 0.039380
qux one -0.353886 -1.200079 0.493888
three -1.042094 1.079344 -0.153037
two -0.124532 0.114733 1.991793
关于python - 将 MultiIndex 的级别重新索引为 Pandas 中的任意顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037159/
我有一个包含 4 个级别的 MultiIndex 的 pandas 数据框。我正在尝试为每个 1 级索引选择具有不同 4 级索引的行。 例子: In [68]: df = pd.DataFrame({
我有一个数据框,其中包含 Year 和 Month 的多个索引,如下所示 | |Value Year |Month| | 1 | 3 1992 | 2 | 5
我有一个带有 3 级 MultiIndex 的 pandas 数据框。我正在尝试根据对应于两个级别的值列表提取此数据帧的行。 我有这样的事情: ix = pd.MultiIndex.from_prod
我想将具有多索引的数据帧中的所有列除以另一个具有多索引的数据帧,更小一级。前两个水平的蛾指数是相同的。并且应该播放第三级。 df_0 = pd.DataFrame( { "col0": [ 1
我想知道是否有人可以帮助我解决这个问题。 如果我有一个简单的数据框: one two three four 0 A 1 a 1 1 A 2 b
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
我希望创建一个新的 DataFrame,与基于芯片的设备 A 和 B 的结果相对应。 以下是我创建 DataFrame 的代码: import numpy as np import pandas as
请让我知道如何将两个 DataFrame 与排序的 MultiIndexes 连接起来,以便结果具有排序的 MultiIndex。 由于两者都是排序的,算法必须根据两个 DataFrame 中的总行数
基本场景 对于推荐服务,我正在针对一组用户-项目交互训练矩阵分解模型 (LightFM)。为了使矩阵分解模型产生最佳结果,我需要将我的用户 ID 和商品 ID 映射到从 0 开始的连续整数 ID 范围
好的,所以我有一个数据框,其中包含时间序列数据,每列都有一个多行索引。这是数据的样例,它是 csv 格式的。加载数据在这里不是问题。 我想要做的是能够创建一个箱线图,其中包含根据多索引特定行中的不同类
我有这个数据框: df = pd.DataFrame({'NUMBER_1': {('2019-07', 'A'): 4, ('2019-07', 'D'): 2, ('2019-08', 'A'):
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有点难以对用于我的一个数据集的列的 pandas MultiIndex 进行排序: MultiIndex(levels=[['Jan', 'Feb', 'Mar', 'Apr', 'May', 'J
我有一个列表列表,我想将其制成多索引 Pandas 数据框,然后可以合并到原始 Pandas 数据框。列表的每一行都包含一个观察值,列表中的值由与该观察值匹配的相应行组成。 这是我所拥有的一个简单版本
我有以下 DataFrame(从我无法更改的 CSV 文件中读取): df = pd.DataFrame([['low', 0.5, 123, 0.8, 123],
我正在创建一个 MultiIndex.from_product(),但它必须是来自两个单独的 MultiIndexes 的唯一值的乘积。我下面的解决方案有效,但我想知道是否有更优雅的解决方案。 fro
我有一个很大的pd.DataFrame,a,看起来像: bid TIT IM Equity HELN SE Equity FHZN SE Equity GLEN LN Equity f
我创建了一个带有多索引的数据框,其中一个索引是一个元组。我无法使用 .loc 进行索引。 这是数据框: a = [['a','a','a','b','b','b','c','c'],[('one',2
我有以下数据框 A B C D E F Algo T X
我在使用 MultiIndex 和 stack() 时遇到问题。以下示例基于 solution from Calvin Cheung在 StackOvervlow 上。 === multi.csv =
我是一名优秀的程序员,十分优秀!