[英]How to plot 5 largest values obtained by dividing one column by another with pandas?
I have a dataset that looks like this: 我有一个看起来像这样的数据集:
variety|points|price
a | 80 | 5
b | 85 | 6
b | 70 | 8
and so on. 等等。
I would like to create a barplot using seaborn that has variety on the x-axis and points/price ratio on the y-axis. 我想使用seaborn创建一个在x轴上具有多种变化并且在y轴上具有点/价格比的条形图。 I have about 150k rows, so I only want to display the 5 best points/price ratios. 我大约有15万行,所以我只想显示5个最佳点/价格比。
This was my idea using another column called result: 这是我的想法,使用另一列称为result的列:
df["Result"] = df["points"]/df["price"]
ax = sns.barplot(x="variety", data=df, order=df["Result"].iloc[:5].index)
which does not work. 这不起作用。
I will be glad for any advice. 我将很高兴为您提供任何建议。
You could try to filter out the first 5 largest values using nlargest . 您可以尝试使用nlargest过滤掉前5个最大值 。
largest_five= df.nlargest(5,'Result')
Then plot it 然后画
ax = sns.barplot(x="variety",y='Result', data=largest_five)
Let me know if this works. 让我知道这个是否奏效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.