2017-07-29 88 views
-3

我有下面的Pandas dataframe。它在「段名稱」和「變量」列中具有兩級索引。熊貓中的多重索引

        mean 
seg1 daily_time_spend_on_sight  25 
     age       36 
     area_income     1250 
     clicked_on_ad     250 
seg2 daily_time_spend_on_sight  10 
     age       26 
     area_income     950 
     clicked_on_ad     125 

我需要改變0級指數出現在all the records pertaining to it:

        mean 
seg1 daily_time_spend_on_sight  25 
seg1 age       36 
seg1 area_income     1250 
seg1 clicked_on_ad     250 
seg2 daily_time_spend_on_sight  10 
seg2 age       26 
seg2 area_income     950 
seg2 clicked_on_ad     125 
+0

有你到目前爲止嘗試過什麼 – EsotericVoid

+1

'.reset_index()'將起作用 – GiantsLoveDeathMetal

回答

2

如果你有一個像多指數低於一個數據集可以使用.reset_index()

arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], 
     ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] 
tuples = list(zip(*arrays)) 
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) 
s = pd.Series(np.random.randn(8), index=index) 
print(s) 
 
first second 
bar one  -0.632949 
     two  -1.418744 
baz one  -1.318791 
     two  0.194042 
foo one  -0.139960 
     two  0.971686 
qux one  -0.257964 
     two  1.911748 
dtype: float64 

s.reset_index()會給

 
first second   0 
0 bar one -0.632949 
1 bar two -1.418744 
2 baz one -1.318791 
3 baz two 0.194042 
4 foo one -0.139960 
5 foo two 0.971686 
6 qux one -0.257964 
7 qux two 1.911748 

您還可以使用pd.option_context('display.multi_sparse', False)

with pd.option_context('display.multi_sparse', False): 
    print(s) 

輸出:

 
first second 
bar one  1.157404 
bar two  -0.000333 
baz one  -0.774613 
baz two  -1.962658 
foo one  1.337555 
foo two  0.856814 
qux one  0.506146 
qux two  0.755346 
dtype: float64 

有關多索引的更多信息,你可以訪問here

希望它可以幫助