Convert categorical data into various columns for plotting in pandas
Question: I'm new to `python` and `pandas` but I've used `R` in the past for data
analysis. I have a simple dataset:

    
    
    df.head()
    
    Sequence      Level    Count
           1       Easy        5
           1     Medium        7
           1       Hard        9
    

I would like to convert this to:

    
    
    Sequence    Easy    Medium    Hard
           1       5         7       9
    

In `R`, I could simply do this by using the `reshape2` package. In `python` it
seems like one of my options is to create dummy variables using `get_dummies`
but that would still generate multiple rows for the same `Sequence` in my
case. Is there an easy way of achieving my resultset?

I'm finally trying to plot it using:

    
    
    import matplotlib.pyplot as plt
    
    
    df.plot(kind='bar', stacked=True)
    
    plt.show()
    

Any help would be appreciated.


Answer: You could use pandas [`pivot_table`](http://pandas.pydata.org/pandas-
docs/stable/generated/pandas.pivot_table.html):

    
    
    In [1436]: pd.pivot_table(df, index='Sequence', columns='Level', values='Count')
    Out[1436]: 
    Level     Easy  Hard  Medium
    Sequence                    
    1            5     9       7
    

Then you could plot it:

    
    
    df1 = pd.pivot_table(df, index='Sequence', columns='Level', values='Count')
    df1.plot(kind='bar', stacked=True)
    

[![enter image description
here](http://i.stack.imgur.com/Zmtlh.png)](http://i.stack.imgur.com/Zmtlh.png)

