Reputation: 833
I've got an Excel Worksheet that has columns of specific statuses, I need a count of each status and then I need it to be graphed in a bar plot. Example:
Status_1 Status_2 Status_3
Active Abandoned Active
Inactive Abandoned Active
Currently I've been using the following code:
import pandas as pd, matplotlib.pyplot as plt
report = r"\\Myserver\Reports\Report.xlsx"
df1 = pd.read_excel(report, sheetname=1)
df = df1[['Status_1', 'Status_2', 'Status_3']].copy()
val1 = df['Status_2'].value_counts().to_frame()
vals1 = pd.DataFrame(val1)
fig, axes = plt.subplots(nrows=2, ncols=2)
vals1.plot(kind='bar', ax=axes[0,0])
So my problem now is that the graph displays properly, but the columns on the X axis are labeled with their statuses (Active/Inactive) and the legend shows the column name (Status_1). I'd like this to be reversed and have the X axis only show "Status_1" and the legend to be color coded with two choices of "Active" and "Inactive".
Because I'm doing this with value counts it's gotten be very confused and turned around. Is there any way of doing this?
Upvotes: 1
Views: 3116
Reputation: 21873
You can use seaborn, along with reshaping you dataframe into a tidy form
import seaborn as sns
import pandas as pd
In [26]: df
Out[26]:
Status_1 Status_2 Status_3
0 Active Abandoned Active
1 Inactive Abandoned Abandoned
2 Active Abandoned Active
3 Inactive Inactive Active
4 Active Abandoned Inactive
5 Abandoned Abandoned Active
In [27]: df2 = pd.melt(df) # convert into tidy data
In [28]: df2
Out[28]:
variable value
0 Status_1 Active
1 Status_1 Inactive
2 Status_1 Active
3 Status_1 Inactive
4 Status_1 Active
[...]
15 Status_3 Active
16 Status_3 Inactive
17 Status_3 Active
In [29]: sns.factorplot(data=df2, x="variable", kind="count", hue="value")
Or:
sns.factorplot(data=df2, x="variable", kind="count", col="value")
HTH
Upvotes: 2
Reputation: 1597
I hope this will help:
a = pd.DataFrame([['a','i'],['a','i'],['i','a']], columns=["S1", "S2"])
a.apply(lambda x: x.value_counts()).transpose().plot.bar()
Result:
Explanation:
a.apply(lambda x: x.value_counts()) - creates a dataframe of counted values from your data,
.transpose() - transpose index and columns (in case if you want to go back to having Status1, Status2 as your bars and Active/Inactive as x-label, just remove this method,
plot.bar() - just plots your DataFrame
Upvotes: 1