python-根据0级索引对多索引Pandas DataFrame的1级索引进行自定义排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-根据0级索引对多索引Pandas DataFrame的1级索引进行自定义排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2061字,纯文字阅读大概需要3分钟。
内容图文
![python-根据0级索引对多索引Pandas DataFrame的1级索引进行自定义排序](/upload/InfoBanner/zyjiaocheng/668/575755ff353647c087f00412aca48e39.jpg)
我有一个multindex DataFrame,df:
arrays = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'one', 'two', 'three', 'four', 'one', 'two']]
df = pd.DataFrame(np.ones([8, 4]), index=arrays)
看起来像:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
现在,我需要将“ baz”子级别分类为新的顺序,以创建类似于df_end的内容:
arrays_end = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'two', 'four', 'three', 'one', 'one', 'two']]
df_end = pd.DataFrame(np.ones([8, 4]), index=arrays_end)
看起来像:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz two 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
one 1.0 1.0 1.0 1.0
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
我以为我可以重新索引baz行:
new_index = ['two','four','three','one']
df.loc['baz'].reindex(new_index)
这使:
0 1 2 3
two 1.0 1.0 1.0 1.0
four 1.0 1.0 1.0 1.0
three 1.0 1.0 1.0 1.0
one 1.0 1.0 1.0 1.0
…并将这些值重新插入到原始DataFrame中:
df.loc['baz'] = df.loc['baz'].reindex(new_index)
但是结果是:
0 1 2 3
bar one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
baz one NaN NaN NaN NaN
two NaN NaN NaN NaN
three NaN NaN NaN NaN
four NaN NaN NaN NaN
foo one 1.0 1.0 1.0 1.0
two 1.0 1.0 1.0 1.0
这不是我要找的东西!所以我的问题是如何使用new_index对baz索引中的行进行重新排序.任何建议将不胜感激.
解决方法:
编辑:(以适合所需的布局)
arrays = [['bar', 'bar', 'baz', 'baz', 'baz', 'baz', 'foo', 'foo'],
['one', 'two', 'one', 'two', 'three', 'four', 'one', 'two']]
df = pd.DataFrame(np.arange(32).reshape([8, 4]), index=arrays)
new_baz_index = [('baz', i) for i in ['two','four','three','one']]
index = df.index.values.copy()
index[df.index.get_loc('baz')] = new_baz_index
df.reindex(index)
df.index.get_loc(‘baz’)将得到baz零件的位置作为切片对象,我们仅在其中替换零件.
内容总结
以上是互联网集市为您收集整理的python-根据0级索引对多索引Pandas DataFrame的1级索引进行自定义排序全部内容,希望文章能够帮你解决python-根据0级索引对多索引Pandas DataFrame的1级索引进行自定义排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。