Код: Выделить всё
dates=pd.date_range("2020-02-01","2021-02-01",freq="MS")
features=["foo","bar"]
cols=pd.MultiIndex.from_product([features,dates],names=["features","dates"])
projects=["p1","p2","p3"]`
df=pd.DataFrame(index=projects,columns=cols,data=np.random.randn(3,26))`
Код: Выделить всё
level_0=list(map(lambda t:t[0],df.columns.to_numpy()))
level_1=list(map(lambda t:t[1],df.columns.to_numpy()))
ind=[i+"_"+j.strftime("%Y_%m") for i,j in zip(level_0,level_1)]
Код: Выделить всё
(
df
.reindex(columns=ind)
)
Я ожидал сохранить те же значения. Поскольку индексы эквивалентны, никакие данные не должны быть потеряны.
Подробнее здесь: https://stackoverflow.com/questions/786 ... ized-index