0 6 1689.306931
6 345.198020
6 226.217822
6 34.574257
6 14.000000
...
3 6 1.077353
6 1.116176
6 1.078431
6 1.049020
6 0.980294
這是我的多索引系列my_df,我可以在其中使用 my_df.loc[0] 訪問每個相同的索引
6 1689.306931
6 345.198020
6 226.217822
6 34.574257
6 14.000000
6 63.683168
6 60.158416
6 60.198020
6 18.811881
6 22.316832
dtype: float64
考慮到每個系列的長度并不完全相同,我如何將這個多索引系列轉換為不拋出錯誤的資料幀:
ValueError:無法從重復軸重新索引
pd.unstack() 拋出:
ValueError:索引包含重復條目,無法重塑
uj5u.com熱心網友回復:
嘗試使用 列舉一層內的行groupby.cumcount,然后取消堆疊:
# your first level seem to be identical, just drop it
(df.reset_index(level=1)
.set_index(df.groupby(level=0).cumcount(), append=True)
.unstack()
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312997.html
