1
舉一個簡單的pd.Series
:在多指標與多指標刪除重複指數無論爲了
#create the multiindex and data
mult = pd.MultiIndex.from_product([[1,2,3],[1,2,3]],names=['factor1','factor2'])
data = np.arange(1,4)*np.arange(1,4)[:,np.newaxis]
#create the series
ser = (pd.Series(data.ravel(),
index=mult,
name='product')
.sort_values(ascending=False))
print(ser)
factor1 factor2
3 3 9
2 6
2 3 6
2 4
3 1 3
1 3 3
2 1 2
1 2 2
1 1
Name: product, dtype: int64
如何可以複製指數,不分順序,刪除,因此,最後的系列是
factor1 factor2
3 3 9
2 6
2 2 4
3 1 3
2 1 2
1 1 1
Name: product, dtype: int64
想法是2*3
和3*2
是相同的因素,所以我們想擺脫一個。我試過drop_duplicates
,但是這消除了任何重複的產品,不管它們的索引如何(因此1*0
和2*0
將被視爲重複項)。
哈克,但工作。謝謝! – user1717828