英文:
Plotting pandas dataframe series with seaborn
问题
我是新手使用 Seaborn 和 Pandas
我的数据框:
df
搜索 A B C D 语言
最适合 Netflix 的电视 51 7.5 25.7 电视 英语
最适合 Firestick 的电视 42 6.3 34.77 电视 西班牙语
便宜的电视 32 2.7 69.33 便宜 英语
便宜的电视 44 14.7 74.14 最佳 法语
...
我正在学习如何使用 Seaborn 绘制数据图表。
我的目标是能够绘制:
- 每列的出现次数,例如柱状图或其他图表,其中值将是列的
value_counts()
。 - 每列的最大值,例如 - 列
A
和B
在类别语言
下的最大值。 - 列
A
在类别D
下的总和。
我应该首先进行计算以获取绘图所需的数字,还是有更巧妙的方法可以绘制带有 Seaborn 的 Pandas 数据框?根据 Seaborn 的文档,它是专门为与 Pandas 数据框良好配合工作而设计的。
我尝试过的内容
count = df['语言'].value_counts()
head = count.head(5)
sns.barplot(x=head, y=count, data=df)
plt.show()
这会绘制前五个语言类别。但是,我不知道如何绘制目标部分中的第二和第三点。
感谢您的建议。
英文:
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:
- Occurence count per column, for example bar or any other plot where the values would be the
value_counts()
of the column - The maximum values per column, for example - maximum values of columns
A
andB
per categoryLanguage
- Sum of column
A
per categoryD
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.
答案1
得分: 1
以下是翻译好的内容:
最大值按语言分组:
grouped_data = df.groupby('语言')[['A', 'B']].max().reset_index()
sns.barplot(x='语言', y='A', data=grouped_data)
sns.barplot(x='语言', y='B', data=grouped_data)
按类别D汇总列A的总和:
sns.barplot(x='D', y='A', data=df.groupby('D').A.sum().reset_index())
英文:
Maximum values per language:
grouped_data = df.groupby('Language')[['A', 'B']].max().reset_index()
sns.barplot(x='Language', y='A', data=grouped_data)
sns.barplot(x='Language', y='B', data=grouped_data)
Sum of column A per category D:
sns.barplot(x='D', y='A', data=df.groupby('D').A.sum().reset_index())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论