Plotting pandas dataframe series with seaborn

huangapple go评论119阅读模式
英文:

Plotting pandas dataframe series with seaborn

问题

我是新手使用 Seaborn 和 Pandas

我的数据框:

df

  1. 搜索 A B C D 语言
  2. 最适合 Netflix 的电视 51 7.5 25.7 电视 英语
  3. 最适合 Firestick 的电视 42 6.3 34.77 电视 西班牙语
  4. 便宜的电视 32 2.7 69.33 便宜 英语
  5. 便宜的电视 44 14.7 74.14 最佳 法语
  6. ...

我正在学习如何使用 Seaborn 绘制数据图表。

我的目标是能够绘制:

  1. 每列的出现次数,例如柱状图或其他图表,其中值将是列的value_counts()
  2. 每列的最大值,例如 - 列AB在类别语言下的最大值。
  3. A在类别D下的总和。

我应该首先进行计算以获取绘图所需的数字,还是有更巧妙的方法可以绘制带有 Seaborn 的 Pandas 数据框?根据 Seaborn 的文档,它是专门为与 Pandas 数据框良好配合工作而设计的。

我尝试过的内容

  1. count = df['语言'].value_counts()
  2. head = count.head(5)
  3. sns.barplot(x=head, y=count, data=df)
  4. plt.show()

这会绘制前五个语言类别。但是,我不知道如何绘制目标部分中的第二和第三点。

感谢您的建议。

英文:

I am new to Seaborn and Pandas

My DataFrame:

df

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

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

  1. count = df['Language'].value_counts()
  2. head = count.head(5)
  3. sns.barplot(x=head, y=count, data=df)
  4. 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.

答案1

得分: 1

以下是翻译好的内容:

最大值按语言分组:

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

Plotting pandas dataframe series with seaborn

  1. sns.barplot(x='语言', y='B', data=grouped_data)

Plotting pandas dataframe series with seaborn

按类别D汇总列A的总和:

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

Plotting pandas dataframe series with seaborn

英文:

Maximum values per language:

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

Plotting pandas dataframe series with seaborn

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

Plotting pandas dataframe series with seaborn

Sum of column A per category D:

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

Plotting pandas dataframe series with seaborn

huangapple
  • 本文由 发表于 2020年1月6日 21:51:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定