Try this runnable code:
import pandas as pd
from io import StringIO
from matplotlib import pyplot
data2 ="""Index Name Detection
0 name1 0
1 name2 0
2 name3 0
3 name1 1
4 name3 1
5 name2 1
6 name1 1
7 name1 0"""
df2 = pd.read_csv(StringIO( data2 ), sep='\s+', index_col='Index', engine='python')
print(df2)
result = df2.groupby(['Name']).count()
print()
print(result)
result.plot(kind='bar')
Output text:
Name Detection
Index
0 name1 0
1 name2 0
2 name3 0
3 name1 1
4 name3 1
5 name2 1
6 name1 1
7 name1 0
Detection
Name
name1 4
name2 2
name3 2
Output plot:
Edit
Updated code:
import pandas as pd
from io import StringIO
from matplotlib import pyplot
data2 ="""Index Name Detection
0 name1 0
1 name2 0
2 name3 0
3 name1 1
4 name3 1
5 name2 1
6 name1 1
7 name1 0"""
df2 = pd.read_csv(StringIO( data2 ), sep='\s+', index_col='Index', engine='python')
result = df2.groupby(by=['Name','Detection']).size().reset_index()
result.rename(columns={0:"count"}, inplace=True)
# plot bar graph
result.set_index(["Name","Detection"])['count'].unstack().plot.bar()
Output plot:
Note that
result.set_index(["Name","Detection"])
is the dataframe in this form:
count
Name Detection
name1 0 2
1 2
name2 0 1
1 1
name3 0 1
1 1
and it is the essence of your question rather than the bar plot of it. The dataframe now has 2 indexes that reveal the count
values clearly for each paired indexes of (name, detection), more intuitive than the original. The 2-indexes dataframe can be used to created the required bar plot more easily.
You miss to focus on the processing of the dataframe and make people think that you ask for something that already have good answer.
Thanks, but I want to plot the count of "Detection" (0's red color and 1's blue color) as y-axsis, based on each name. For example for name1 -->2 (0's) and name1 -->2 (1's)
@meee You can edit your question to enable reopening it. If you can do appropriately I will vote to open it.
@meee People who vote to close think that your dataframe is ready to plot such bargraph, and there is already a good answer to your question. In fact it is not ready or not convenient to do so. It needs some manipulation that I demonstrate. The resulting dataframe with paird-indexes is a result, and it is more important than the plot. With that form of dataframe, you need one-liner code to plot what you need.