Jonas Palačionis
Jonas Palačionis

Reputation: 4842

Plotting pandas dataframe series with seaborn

I am new to Seaborn and Pandas

My DataFrame:

df

Search                      A       B       C       D           Language
Best TV for netflix         51      7.5     25.7    TV          en
Best TV for firestick       42      6.3     34.77   TV          es
TV cheap                    32      2.7     69.33   Cheap       en
Cheap TV                    44      14.7    74.14   Best        fr
...

I am learning to plot data with seaborn.

My goal is to be able to plot:

  1. Occurence count per column, for example bar or any other plot where the values would be the value_counts() of the column
  2. The maximum values per column, for example - maximum values of columns A and B per category Language
  3. Sum of column A per category D

Should I first do calculations to get the numbers I need for the plots or are there more subtle ways of plotting pandas dataframes with seaborn as per seaborn documentation it is said that it was made to work well with pandas dataframes.

What I've tried

count = df['Language'].value_counts()
head = count.head(5)
sns.barplot(x=head, y=count, data=df)
plt.show()

Which plots the top5 language categories. But I don't know how to plot the second and third points in my goal section.

Thank you for your suggestions.

Upvotes: 1

Views: 294

Answers (1)

ignoring_gravity
ignoring_gravity

Reputation: 10476

Maximum values per language:

grouped_data = df.groupby('Language')[['A', 'B']].max().reset_index()
sns.barplot(x='Language', y='A', data=grouped_data)

enter image description here

sns.barplot(x='Language', y='B', data=grouped_data)

enter image description here

Sum of column A per category D:

sns.barplot(x='D', y='A', data=df.groupby('D').A.sum().reset_index())

enter image description here

Upvotes: 1

Related Questions