如何在python中绘制具有非数字值的表? [英] How to plot a table with non-numeric values in python?

查看:56
本文介绍了如何在python中绘制具有非数字值的表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含三列的数据框,我想将其绘制成图表.我需要图表来显示整个覆盖范围"中有多少(或百分比)是"行.我很难创建图表,因为这些都是非数字值.有人可以建议吗?

I've got a dataframe with three columns, which I'd like to plot into a chart. I need the chart to show how many (or what's the %) of "Yes" rows from the whole "coverage". I struggle with creating a chart as these are non-numeric values. Can anyone advise, please?

parent      rule        coverage
AFKS RM     AFKS RM     Yes
AFLT RM     AFLT RM     Yes
AGRO LI     NaN         No
AKRN RM     AKRN RM     Yes
ALRS RM     ALRS RM     Yes
BANE RM     BANE RM     Yes

我查看了 Stackoverflow 中的其他答案之一并尝试了以下方法,但它给了我TypeError: Empty 'DataFrame': no numeric data to plot".

I looked at one of the other answers in Stackoverflow and tried the below but it gives me "TypeError: Empty 'DataFrame': no numeric data to plot".

grouped = df.groupby(['coverage'])['parent']grouped.sum().plot(kind='bar')

我也尝试计算覆盖率,但是不确定这是否是正确的方法以及如何从此处进行绘制.

I also tried to calculate percentage of coverage, but not sure how if that's the right way and how to plot from here.

df = df[result['coverage']=='Yes'].count() /df['coverage'].count()

推荐答案

找到百分比的最简单方法是使用均值:

The easiest way to find percentage is to use mean:

(df.loc[:,'coverage'] == 'Yes').mean()

输出:

0.83333333333333337

绘图:

df.groupby('coverage')['coverage'].count().plot.pie(autopct='%.2f',figsize=(5,5))

df.groupby('coverage')['coverage'].count().plot.pie(autopct='%.2f',figsize=(5,5))

这篇关于如何在python中绘制具有非数字值的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆