3

I have a data frame similar to this

import pandas as pd
df = pd.DataFrame([['1','3','1','2','3','1','2','2','1','1'], ['ONE','TWO','ONE','ONE','ONE','TWO','ONE','TWO','ONE','THREE']]).T
df.columns = [['age','data']]
print(df)   #printing dataframe.

I performed the groupby function on it to get the required output.

df['COUNTER'] =1       #initially, set that counter to 1.
group_data = df.groupby(['age','data'])['COUNTER'].sum() #sum function
print(group_data)

now i want to plot the out using matplot lib. Please help me with it.. I am not able to figure how to start and what to do. I want to plot using the counter value and something similar to bar graph

2 Answers 2

7

Try:

group_data = group_data.reset_index()

in order to get rid of the multiple index that the groupby() has created for you.

Your print(group_data) will give you this:

In [24]: group_data = df.groupby(['age','data'])['COUNTER'].sum() #sum function

In [25]: print(group_data)
age  data 
1    ONE      3
     THREE    1
     TWO      1
2    ONE      2
     TWO      1
3    ONE      1
     TWO      1
Name: COUNTER, dtype: int64

Whereas, reseting will 'simplify' the new index:

In [26]: group_data = group_data.reset_index()

In [27]: group_data
Out[27]: 
  age   data  COUNTER
0   1    ONE        3
1   1  THREE        1
2   1    TWO        1
3   2    ONE        2
4   2    TWO        1
5   3    ONE        1
6   3    TWO        1

Then depending on what it is exactly that you want to plot, you might want to take a look at the Matplotlib docs

EDIT

I now read more carefully that you want to create a 'bar' chart.

If that is the case then I would take a step back and not use reset_index() on the groupby result. Instead, try this:

In [46]: fig = group_data.plot.bar()

In [47]: fig.figure.show()

I hope this helps

Sign up to request clarification or add additional context in comments.

5 Comments

and then use the plot()?
@karankothari If you found this useful please accept my answer, so it's clear that this question has been answered.
I still need some idea about the plot.. So waitin on that
My suggestion is not what you are looking for? Maybe add some more details on what it is you want the plot to look like then(?)
I want the age on one axis and data on the other.. Should be a bar graph.. The graph should show relation between age and data
2

Try with this:

# This is a great tool to add plots to jupyter notebook
% matplotlib inline
import pandas as pd
import matplotlib.pyplot as plt

# Params get plot bigger
plt.rcParams["axes.labelsize"] = 16
plt.rcParams["xtick.labelsize"] = 14
plt.rcParams["ytick.labelsize"] = 14
plt.rcParams["legend.fontsize"] = 12
plt.rcParams["figure.figsize"] = [15, 7]

df = pd.DataFrame([['1','3','1','2','3','1','2','2','1','1'], ['ONE','TWO','ONE','ONE','ONE','TWO','ONE','TWO','ONE','THREE']]).T
df.columns = [['age','data']]
df['COUNTER'] = 1

group_data = df.groupby(['age','data']).sum()[['COUNTER']].plot.bar(rot = 90) # If you want to rotate labels from x axis
_ = group_data.set(xlabel = 'xlabel', ylabel = 'ylabel'), group_data.legend(['Legend']) # you can add labels and legend

2 Comments

Is there a way to make the graph bigger? the data that i have provided is but a sample of the original.. and I require the graph to be much bigger.. even saving it to a file will help..
Adding some params, yes. I edited the answer with some params will get the plot bigger

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.