Leslie Tate
Leslie Tate

Reputation: 570

Matplotlib plotting dataframe

I'm trying to learn how to plot dataframes. I read in a csv and have the following columns:

 cost, model, origin, year
--------------------------
200    x1     usa     2020
145    x1     chn     2020
233    x1     usa     2020
122    x2     chn     2020
583    x2     usa     2020
233    x3     chn     2020 
201    x3     chn     2020

I'm trying to create a bar plot and only want to plot the average cost per model.

Here's my attempt, but I dont think im on the right track:

df = df.groupby('cost').mean()
plt.bar(df.index, df['model'])
plt.show()

Upvotes: 0

Views: 70

Answers (2)

perl
perl

Reputation: 9941

You can groupby model, then calculate the mean of cost and plot it:

df.groupby('model')['cost'].mean().plot.bar()

Output:

picture


Or with seaborn:

sns.barplot(data=df, x='model', y='cost', ci=None)

Output:

picture

Upvotes: 1

not_speshal
not_speshal

Reputation: 23146

You can use the pandas plot function like so:

df.plot.bar(x='model', y='cost')

Upvotes: 1

Related Questions