首页 > 解决方案 > 为循环中的列打印 value_counts

问题描述

我正在使用散景打印多列的水平条形图。我用for循环来做同样的事情。这是代码,但是当我运行它时。我得到这个错误。我是散景的初学者。请帮忙。

chart_cols = ['respondent_age respondent_gender respondent_edu respondent_occupation Religion Caste_cat CM_choice Likely_winner'.split()]
chart_cols
for f in chart_cols:
    count = df[f].value_counts()

    p = figure(plot_height=400, plot_width=400, title='Chart',toolbar_location=None)

    p.title.align = "right"
    p.xaxis.axis_label = 'Number of respondents'
    p.yaxis.axis_label = 'Something'

    p.hbar(y=sorted(df[f].unique()), height=0.7, left=0,
          right=count, color=Category20,
           alpha=0.7)
    show(p)
    print('Done')

AttributeError: 'DataFrame' object has no attribute 'value_counts'

为了编写上面的代码,我使用了散景文档和可用的数据。我试过

使用.运算符,但错误是相同的。在那种情况下,它说没有.属性 例如df.f.value_counts()

我在做什么错?请帮忙。

标签: pythonpandasbokeh

解决方案


您的 chart_cols 中有一对额外的方括号。将第一行替换为

chart_cols = 'respondent_age respondent_gender respondent_edu respondent_occupation Religion Caste_cat CM_choice Likely_winner'.split()

推荐阅读