pandas 排序和限制条形图上显示的条形数量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38338396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort and limit number of bars to display on bargraph
提问by zelda1234
I have a dataset of traffic violations and want to display only the top 10 violations per month on a bargraph. Can I limit the number of bars after sorting values to display only the top 10? There are 42 different column names of traffic violations.
我有一个交通违规数据集,并且只想在条形图上显示每月前 10 次的违规行为。我可以在对值进行排序后限制条数以仅显示前 10 条吗?交通违章有42个不同的列名。
month_jan = df[df.MonthName == "Jan"]
month_jan[feature_cols].sum().sort_values(ascending=0).plot(kind='bar')
Feature_cols
is a list of all 42 column names that correspond to traffic violations.
Feature_cols
是与交通违规对应的所有 42 个列名称的列表。
Thanks!
谢谢!
采纳答案by mechanical_meat
This will work:
这将起作用:
month_jan[feature_cols].sum().sort_values(ascending=0)[:10].plot(kind='bar')
回答by ibab
Series
objects have a .head
method, just like DataFrame
s (docs).
This allows you to select the top N items very elegantly with data.head(N)
.
Here's a complete working example:
Series
对象有一个.head
方法,就像DataFrame
s ( docs)。这使您可以非常优雅地选择前 N 个项目data.head(N)
。这是一个完整的工作示例:
import pandas as pd
df = pd.DataFrame({
'feature1': [0, 1, 2, 3],
'feature2': [2, 3, 4, 5],
'MonthName': ['Jan', 'Jan', 'Jan', 'Feb']
})
feature_cols = ['feature1', 'feature2']
month_jan = df[df.MonthName == "Jan"]
top10 = month_jan[feature_cols].sum().sort_values(ascending=0).head(10)
top10.plot(kind='bar')