Reputation: 577
I have a dataset something like this:
ID TAG TYPE
1 A GT
2 B NGT
3 C XT
4 A NGT
I want to plot a graph showing the number of NGT for each tag. How do I achieve this? I could filter by tags and count but I want to do this directly using pandas and/or seaborn/matplotlib.
Upvotes: 0
Views: 576
Reputation: 743
Create a boolean column initially that shows whether NGT
is True
or False
.
df["NGT_val"]= df["TYPE"].str.contains("NGT", regex=False)
Output:
TAG TYPE NGT
0 A GT False
1 B NGT True
2 C XT False
3 A NGT True
Then you can do pd.groupby()
grouped=df.groupby("TAG").agg(num_true=pd.NamedAgg(column="NGT",aggfunc=sum))
Output: # shows the number of "NGT"s present for each TAG.
num_true
TAG
A 1
B 1
C 0
# Then plot the chart.I just did the bar plot.
grouped['num_true'].plot(kind="bar")
Upvotes: 1