python - 为循环中的列打印 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()
我在做什么错?请帮忙。
解决方案
您的 chart_cols 中有一对额外的方括号。将第一行替换为
chart_cols = 'respondent_age respondent_gender respondent_edu respondent_occupation Religion Caste_cat CM_choice Likely_winner'.split()
推荐阅读
- ios - 当单击 xamarin 表单 ios 中的非活动推送通知时,ios 应用程序卡在启动屏幕中
- python - 在 Zapier 代码步骤中创建嵌套 JSON 以传递到 Formstack Documents
- javascript - 我可以使用 html 和 firebase 在一个界面中添加 2 个表吗?
- google-sheets - 建立两个日期之间不工作的员工报告的公式
- list - 如何在 Kotlin 的 MutableList 中交换元素?
- wordpress - 如何将 wordpress $wpbd 与自定义表格一起使用?
- matlab - MATLAB 全局变量和脚本
- python - 将字典的字典转换为数据框
- python - 如何应用多个条件从数据框(熊猫)中删除/选择特定行?
- veins - 如何为“相扑”和“静脉”添加新属性?