在数据帧中,我想计算每一列的值并使用这些值作为索引。
我想把这个:
Q1 Q2 Q3
Strongly agree Agree Undecided
Undecided Agree More or less disagree
Strongly agree Agree Undecided
Strongly agree Strongly Disagree Disagree
More or less agree Undecided Strongly disagree
进入这个:
Q1 Q2 Q3
Strongly agree 3 0 0
Agree 0 3 0
More or less agree 1 0 0
Undecided 1 1 2
More or less disagree 0 0 1
Disagree 0 0 1
Strongly disagree 0 1 1
熊猫怎么可能这样?
最佳答案
如果您坚持value_counts
,您可以事先使用stack
和groupby
,然后在value_counts
之前致电unstacking
:
df.stack().groupby(level=[1]).value_counts().unstack(0, fill_value=0)
Q1 Q2 Q3
Agree 0 3 0
Disagree 0 0 1
More or less agree 1 0 0
More or less disagree 0 0 1
Strongly Disagree 0 1 0
Strongly agree 3 0 0
Strongly disagree 0 0 1
Undecided 1 1 2
另一种选择是使用
melt
和pivot_table
:(df.melt()
.pivot_table(columns='variable', index='value', aggfunc='size', fill_value=0))
variable Q1 Q2 Q3
value
Agree 0 3 0
Disagree 0 0 1
More or less agree 1 0 0
More or less disagree 0 0 1
Strongly Disagree 0 1 0
Strongly agree 3 0 0
Strongly disagree 0 0 1
Undecided 1 1 2
使用
crosstab
的解决方案:v = df.melt()
pd.crosstab(v['value'], v['variable'])
variable Q1 Q2 Q3
value
Agree 0 3 0
Disagree 0 0 1
More or less agree 1 0 0
More or less disagree 0 0 1
Strongly Disagree 0 1 0
Strongly agree 3 0 0
Strongly disagree 0 0 1
Undecided 1 1 2
关于python - 如何在DataFrame上应用Series.value_counts()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54544084/