本文介绍了如何在DataFrame上应用Series.value_counts()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 在DataFrame中,我想计算每一列的值并将这些值用作索引。In a DataFrame i would like to count the values of each column and use the values as index. 我想转一下:I would like to turn this:Q1 Q2 Q3Strongly agree Agree UndecidedUndecided Agree More or less disagreeStrongly agree Agree UndecidedStrongly agree Strongly Disagree DisagreeMore or less agree Undecided Strongly disagree 此内容: Q1 Q2 Q3Strongly agree 3 0 0Agree 0 3 0More or less agree 1 0 0Undecided 1 1 2More or less disagree 0 0 1Disagree 0 0 1Strongly disagree 0 1 1大熊猫怎么可能?推荐答案如果您坚持使用 value_counts ,则可以使用 stack 和 groupby 事先,然后在取消堆叠 value_counts >:If you insist on value_counts, you can use stack and groupby beforehand and then call value_counts before unstacking:df.stack().groupby(level=[1]).value_counts().unstack(0, fill_value=0) Q1 Q2 Q3Agree 0 3 0Disagree 0 0 1More or less agree 1 0 0More or less disagree 0 0 1Strongly Disagree 0 1 0Strongly agree 3 0 0Strongly disagree 0 0 1Undecided 1 1 2 另一种选择是使用融化和数据透视表:(df.melt() .pivot_table(columns='variable', index='value', aggfunc='size', fill_value=0))variable Q1 Q2 Q3value Agree 0 3 0Disagree 0 0 1More or less agree 1 0 0More or less disagree 0 0 1Strongly Disagree 0 1 0Strongly agree 3 0 0Strongly disagree 0 0 1Undecided 1 1 2 使用交叉表的解决方案:v = df.melt()pd.crosstab(v['value'], v['variable'])variable Q1 Q2 Q3value Agree 0 3 0Disagree 0 0 1More or less agree 1 0 0More or less disagree 0 0 1Strongly Disagree 0 1 0Strongly agree 3 0 0Strongly disagree 0 0 1Undecided 1 1 2 这篇关于如何在DataFrame上应用Series.value_counts()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 15:10