admin管理员组文章数量:1245089
I am preparing a dataset in the "pd-multiindex" format for usage in a library called sktime. It looks like the following.
instances time points a b
0 0 2 -2
1 1 -1
2 1 -2
3 -2 2
4 -2 2
... .. ..
826 9 -1 1
10 -1 0
11 1 0
12 -1 1
13 2 -2
The "instances" and "time points" form a multi-index. I would like to change the starting index of instances from 0 to 826 (and so the ending index would become 826+826=1652)
I am preparing a dataset in the "pd-multiindex" format for usage in a library called sktime. It looks like the following.
instances time points a b
0 0 2 -2
1 1 -1
2 1 -2
3 -2 2
4 -2 2
... .. ..
826 9 -1 1
10 -1 0
11 1 0
12 -1 1
13 2 -2
The "instances" and "time points" form a multi-index. I would like to change the starting index of instances from 0 to 826 (and so the ending index would become 826+826=1652)
Share Improve this question asked Feb 17 at 15:53 Yu ColemanYu Coleman 434 bronze badges1 Answer
Reset to default 1Try this code once
import pandas as pd
# assuming your DataFrame with a MultiIndex
print("Before:", df.index.get_level_values(0).unique())
# Update the MultiIndex: add 826 to the first level
df.index = df.index.map(lambda idx: (idx[0] + 826, idx[1]))
# Check the final answer
print("After:", df.index.get_level_values(0).unique())
本文标签: pythonShift change the index of a dataframeStack Overflow
版权声明:本文标题:python - Shift change the index of a dataframe - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740145143a2231633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论