在Dataframe中仅在索引上显示一次重复的字符串

时间:2016-04-13 09:01:03

标签: python pandas dataframe

我有一个数据框

import pandas as pd

df=pd.DataFrame({
'Foo': ['John','John','John','Steve','Steve','Ted'],
'Score': [4.1,6,5,7,6,0],
'Picotee':[0,1,0,1,0,0]
})

df = df.set_index('Foo', append=True)

print(df)

打印到:

>>> 
         Picotee  Score
 Foo                  
John         0    4.1
John         1    6.0
John         0    5.0
Steve        1    7.0
Steve        0    6.0
Ted          0    0.0

是否可以让输出仅在索引列'Foo'中显示一次重复的条目,所以它看起来像

>>> 
         Picotee  Score
 Foo                  
John         0    4.1
             1    6.0
             0    5.0
Steve        1    7.0
             0    6.0
Ted          0    0.0

1 个答案:

答案 0 :(得分:0)

根据你的评论,你不需要这一行:df = df.set_index('Foo', append=True)所以你可以把它拿出来并使用简单的pivot_table

pd.pivot_table(df, index=['Foo', 'Score'])

输出:

                Picotee
Foo     Score   
John    4.1     0
        5.0     0
        6.0     1
Steve   6.0     0
        7.0     1
Ted     0.0     0