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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:34:08  来源:igfitidea点击:

Sort and limit number of bars to display on bargraph

pythonpandasbar-chart

提问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_colsis 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

Seriesobjects have a .headmethod, just like DataFrames (docs). This allows you to select the top N items very elegantly with data.head(N). Here's a complete working example:

Series对象有一个.head方法,就像DataFrames ( 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')