簡體   English   中英

在熊貓中,如何在多索引的第 2 級重新索引(填充 0)

[英]In pandas, how to reindex(fill 0) in level 2 in multiindex

我有一個具有 2 級索引的數據框:月份和評級。 評級應該是 1,2,3(不要與列 1,2,3 混淆)。 我發現幾個月來,評級可能會丟失。 例如,(人口和 2021-10)只有等級 1,2。 我需要每個月都有評級 1,2,3。 所以我需要為缺失的評分指數填寫 0。 我該怎么做?

我試過 reindex([1,2,3],level='rating'),但不起作用。 我花了幾個小時嘗試。 請幫忙。 謝謝!

在此處輸入圖像描述

在此處輸入圖像描述

您可以使用pd.MultiIndex.from_product創建完整索引:

>>> df
                             1         2         3
(Population)       1  0.436954  0.897747  0.387058
                   2  0.464940  0.611953  0.133941
2021-08(Refreshed) 1  0.496111  0.282798  0.048384
                   2  0.163582  0.213310  0.504647
                   3  0.008980  0.651175  0.400103

>>> df.reindex(pd.MultiIndex.from_product(df.index.levels), fill_value=0)
                             1         2         3
(Population)       1  0.436954  0.897747  0.387058
                   2  0.464940  0.611953  0.133941
                   3  0.000000  0.000000  0.000000  # New record
2021-08(Refreshed) 1  0.496111  0.282798  0.048384
                   2  0.163582  0.213310  0.504647
                   3  0.008980  0.651175  0.400103

更新

我想知道df=df.reindex([1,2,3],level='rating',fill_value=0)不起作用,因為新的索引值 [1,2,3] 無法填充前一個的缺失值評級指數。 通過使用 from_product,它創建了兩個索引的乘積。

事實上它有效。 我的意思是它有效果,但不是你所期望的。 該方法重新索引級別而不是 我來給你展示:

# It seems there is not effect because you don't see 3 and 4 as expected?
>>> df.reindex([1, 2, 3, 4], level='ratings')
                                   0         1         2
                   ratings                              
(Population)       1        0.536154  0.671380  0.839362
                   2        0.729484  0.512379  0.440018
2021-08(Refreshed) 1        0.279990  0.295757  0.405536
                   2        0.864217  0.798092  0.144219
                   3        0.214566  0.407581  0.736905

# But yes something happens
>>> df.reindex([1, 2, 3, 4], level='ratings').index.levels
FrozenList([['(Population)', '2021-08(Refreshed)'], [1, 2, 3, 4]])
                              The level has been reindexed ---^

# It's different from values
>>> df.reindex([1, 2, 3, 4], level='ratings').index.get_level_values('ratings')
Int64Index([1, 2, 1, 2, 3], dtype='int64', name='ratings')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM