I have a df with a regular datetime index. I need to add multiple index levels. What's a pythonic way to achieve this?
import numpy as np
import pandas as pd
idx = pd.date_range(start="2020-01-01", end="2020-01-10")
vals = {"values": np.random.randint(low=1, high=100, size=10)}
df = pd.DataFrame(data=vals, index=idx)
df.index.name = "time"
I need to add, for example, two new index levels. They must prepend the current index level "time". The result should look like this.
values
L0_name L1_name time
L0 value L1 value 2020-01-01 87
2020-01-02 46
2020-01-03 19
2020-01-04 44
2020-01-05 94
2020-01-06 58
2020-01-07 74
2020-01-08 32
2020-01-09 64
2020-01-10 84