How do I build a bar chart based on counting the unique values in a column?
I want to build a bar chart (Bar chart) based on counting the unique values in the column:
count
3
2
3
1
3
1
3
2
5
3
4
3
3
3
4
3
I understand that:
a = pd.unique(df['count']).tolist()
Gives a list of unique values, and:
b = df['count'].value_counts()
Gives a unique value - the frequency of its "occurrence" in the list.
How can this b
be used to make exactly a list of "meetings", so that you can build a diagram:
bars = plt.bar(a, b)
3
2 answers
The easiest way to do this is to use the ready-made library function Seaborn
:
import seaborn as sns
sns.countplot(df['count'])
You can also use a clean matplotlib
, but it will not look so nice:
b = df['count'].value_counts()
plt.bar(b.index, b.values)
6
Author: CrazyElf, 2020-08-26 08:08:24
Alternative option:
df["count"].value_counts().plot.bar(rot=0, grid=True)
PS but the solution suggested by @CrazyElf looks shorter and the result is prettier:
sns.countplot(df['count'])
5
Author: MaxU, 2020-08-26 10:44:43