python - 带有按计数排序的条形返回的熊猫图?
问题描述
我正在使用以下代码创建一个图表,显示我的 df 中出现了多少空值,按日期列排序:
df[df['Gas'].isnull()]['ReportDate_Time'].sort_values().value_counts().plot()
这是它返回的图,没关系,但我宁愿使用条形图。但是,如果我将 'bar' 参数传递给 plot 方法,我会自动让我的条形按总数排序,而不是按 ReportDate_Time 排序,这正是我最初想要的:
df[df['Gas'].isnull()]['ReportDate_Time'].sort_values().value_counts().plot('bar')
我将如何使用条形图并同时按 ReportDate_Time 排序?
解决方案
如果我理解正确,您只需要按报告日期对数据框进行排序,sort_values('Reportdate')
请参阅下面的示例。
dates = pd.date_range(pd.to_datetime('2020-01-21'), pd.to_datetime('2020-02-01'),freq='D')
vals = np.random.randint(0,500,size=len(dates))
df = pd.DataFrame({'ReportDate' : dates, 'count' : vals})
df.sort_values('count',inplace=True)
df.reset_index(drop=True,inplace=True)
print(df)
ReportDate count
0 2020-01-28 135
1 2020-01-30 194
2 2020-01-21 238
3 2020-01-29 316
4 2020-01-31 325
5 2020-01-26 408
6 2020-01-23 450
7 2020-01-22 451
8 2020-01-25 452
9 2020-01-24 454
10 2020-02-01 463
11 2020-01-27 489
df.set_index('ReportDate').plot(kind='bar')
并有一个排序:
df.sort_values('ReportDate',ascending=True).set_index('ReportDate').plot(kind='bar')
推荐阅读
- python - Python中5 X 5相邻栅格像素的平均值
- spring - Jhipster 弹簧生产简介
- c# - C# 基于条件运算符构建表达式树以与 MongoDB 过滤器一起使用
- youtube - 如何从 youtube 预览图像中删除黑色边框(不同的图像黑色边框长度)
- reactjs - 来自任何其他组件的 React Access Loader 组件
- logging - 我可以在 datadog 仪表板的查询小部件中总结不同的日志吗?
- c# - .Net Core 3.1 导入的 WCF 参考忽略枚举值
- firebase - Flutter - 当用户从各自的设备上卸载 iOS 和 Android 应用程序时,删除 Firebase Firestore 数据的云功能
- python - 将 dcc.Interval 动态添加到破折号
- javascript - 移动端Safari中,网站添加到主屏幕,导航页面时顶部和底部显示灰色条,如何使其全屏?