python - 一个索引列到多索引
问题描述
我有一个标题看起来像这样的数据框:
Time Peter_Price, Peter_variable 1, Peter_variable 2, Maria_Price, Maria_variable 1, Maria_variable 3,John_price,...
2017 12 985685466 Street 1 12 4984984984 Street 2
2018 10 985785466 Street 3 78 4984974184 Street 8
2019 12 985685466 Street 1 12 4984984984 Street 2
2020 12 985685466 Street 1 12 4984984984 Street 2
2021 12 985685466 Street 1 12 4984984984 Street 2
以后按组比较变量的最佳多指标是什么,例如哪个人的变量 3 最高或所有人的所有变量 3 的趋势
我认为我需要的是这样的东西,但我接受其他建议(这是我第一个使用多索引的方法)。
Peter Maria John
Price, variable 1, variable 2, Price, variable 1, variable 3, Price,...
Time
解决方案
尝试:
df=df.set_index('Time')
df.columns = pd.MultiIndex.from_tuples([x.split('_') for x in df.columns])
输出:
Peter Maria
Price variable1 variable2 Price variable1 variable3
Time
2017 12 985685466 Street 1 12 4984984984 Street 2
2018 10 985785466 Street 3 78 4984974184 Street 8
2019 12 985685466 Street 1 12 4984984984 Street 2
2020 12 985685466 Street 1 12 4984984984 Street 2
2021 12 985685466 Street 1 12 4984984984 Street 2
推荐阅读
- azure-devops - 带有符号的 Azure DevOps NuGet 源
- javascript - 创建函数时这些方式有什么区别
- php - 使用 laravel 社交名流验证用户的问题
- azure - 在 Log Analytics 查询中显示图表
- android - 错误:找不到正确的提供者
在此 Wrapper Widget 上方 - python - 怎么了?名称错误
- node.js - Azure AppService Kudu 部署在推送时失败
- html - CSS 样式不适用于 datetime-local 元素
- c++ - 如何在 Matlab MEX 文件中正确包含头 C++ 文件(来自 ALGLIB)
- c# - 在 C# 中使用查找模式