Stacked Histogram for pandas dataframe in python

pandas
ipython
python

#1

Hello,I am working on a csv file in pandas. Here there are two columns which look like

Here first column is serial no ,second is Activity code and third is person no
I want a stacked histogram with person no on x-axis where the histogram shows proportion of each activity in each person no.s’bar with different colour so that I can conclude which person has greater participation in which activity.

there are total 5 type of activities with code 1,2,3,4,5
and there 30 person’s no with no. 1,2,3,4…29,30.
Thanks


#2

Hello,
First of all we need to cross-tabulate activity code and person no. to be able to plot the histogram
We can do this as follows
import pandas as pd
import matplotlib.pyplot as plt #import pandas and matplotlib.pyplot libraries

temp1 = pd.crosstab([df3.personno],[df3.Activitycode],rownames=[‘person’],colnames=[‘Activity’])
#Cross-tabulation of person no and activity code

temp1.plot(kind=‘bar’,stacked = True,color=[‘red’,‘green’,‘blue’,‘black’,‘yellow’,‘brown’],grid = False)
plt.show() # plotting stacked histogram


#3

can u show the image in the answer also please


#4

It will look somewhat like this: