Pandas stacked bar chart

前端 未结 2 1388
日久生厌
日久生厌 2021-01-16 06:33

I have the following dataset:

SessionId    Query
   1           a   
   1           b
   2           a
   3           b
   3           b
   3           c
            


        
2条回答
  •  青春惊慌失措
    2021-01-16 06:43

    crosstab should do the job if I understand your question correctly:

    import pandas as pd
    
    data = pd.DataFrame({'SessionId': [1, 1, 2, 3, 3, 3, 3], 
                         'Query': ['a', 'b', 'a', 'b', 'b', 'c', 'a']})
    
    pd.crosstab(data.SessionId, data.Query).plot.barh(stacked=True)
    

提交回复
热议问题