Issue
The organization is the index and I need to show a horizontal bar plot showing the total number of attempts of experiments conducted by each organization, with the most at the top and the fewest at the bottom.
The data frame are as follows : Organization DateTime Attempts a .... Failed b .... Success b .... Failed a .... Partial Success a .... Success b .... Partial Success
I got the idea that the attempts is categorical data and it has some count_values()
function but
just don't know how to code it?
Solution
Let's assume this input:
np.random.seed(0)
df = pd.DataFrame({'company': np.random.choice(['A', 'B', 'C', 'D'], 50),
'Location': 'non-relevant',
'Outcome': np.random.choice(['Success', 'Failure'], 50),
}).set_index('company')
df.head()
Location Outcome
company
A non-relevant Success
D non-relevant Failure
B non-relevant Failure
A non-relevant Failure
D non-relevant Failure
...
Calculate the counts per group and sort:
>>> df2 = df.groupby('company')['Outcome'].count().sort_values()
>>> df2
company
C 8
A 12
B 12
D 18
Name: Outcome, dtype: int64
plot:
df2.plot.barh()
And here how to calculate with failure/successes:
df2 = df.groupby('company')['Outcome'].value_counts().unstack('Outcome')
df2 = df2.loc[df2.sum(axis=1).sort_values().index]
df2.plot.barh(stacked='True')
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.