计算pandas数据框两列中值的每个组合的出现次数,并绘制一个条形图

时间:2019-06-07 21:05:30

标签: python pandas dataframe visualization

我的数据框中有两列包含二进制值。如果项目获得批准,则project_is_approved列的值为1,否则为0 numbers_in_summary_or_not:存在另一列(名为“ summary”),该列具有构造该列所基于的文本数据。如果汇总中的文本数据中使用了数字,则对应的行将为1,否则为0

现在,我想基于这两列来可视化我的数据:

numbers_in_summary_or_not,project_is_approved

我认为条形图应该起作用,我想我需要显示4个条形图的4个 功能

numbers_in_summary_or_not = 0和project_is_approved = 0:第一个条形码numbers_in_summary_or_not = 0和project_is_approved = 1:第二个barplotnumbers_in_summary_or_not = 1和project_is_approved = 0:第三个barplot数字_in_approved = 1 =

1 个答案:

答案 0 :(得分:0)

可能是您正在尝试执行类似绘制这两列组合计数的操作?

在这种情况下,您可以这样添加一列,将两者结合起来

# a series to use for counting- formatted as cat1_cat2
ts = (df['numbers_in_summary_or_not'].astype(str) + '_' + df['project_is_approved'].astype(str))
# and plot the counts of the groups like
ts.value_counts().plot(kind = 'bar')