gpt4 book ai didi

python - 使用 pandas 总结分类问卷数据

转载 作者:行者123 更新时间:2023-12-01 09:17:15 25 4
gpt4 key购买 nike

从Python开始,我遇到了一个很常见但找不到简单解决方案的问题。我有一些虚构的问卷数据,我想获得有意义的描述。具体来说,对于每个问题,我想知道给出了多少次特定的回答("is"/“也许”/“否”)。

输入:

         Question1   Question2   Question3
Answer1 Maybe Yes Yes
Answer2 No Maybe Yes
Answer3 Maybe Maybe No
Answer4 No Yes Maybe

现在,我想大致了解某个问题的特定答案的次数。首选输出如下:

(首选)输出:

           Yes     Maybe    No
Question1 0 2 2
Question2 2 2 0
Question3 2 1 1

我自己的想法是解决方案必须在“groupby”命令中。到目前为止,我还没有成功获得任何有意义的输出:

df.groupby(['Question1']).sum()
Question2 Question3
Question1
Maybe YesMaybe YesNo
No MaybeYes YesMaybe

我已经生成了虚拟数据:

# Generate data
data = np.array([['','Question1','Question2','Question3'],['Answer1',"Maybe","Yes","Yes"],['Answer2',"No","Maybe","Yes"],['Answer3',"Maybe","Maybe","No"],['Answer4',"No","Yes","Maybe"]])


# convert to pandas dataframe
df = pd.DataFrame(data=data[1:,1:],index=data[1:,0],columns=data[0,1:])

我知道这肯定是一个简单的挑战,但我们将不胜感激任何帮助。

最佳答案

简单

df.apply(pd.value_counts).fillna(0)


Question1 Question2 Question3
Maybe 2.0 2.0 1.0
No 2.0 0.0 1.0
Yes 0.0 2.0 2.0

如果需要,可以转置它df.apply(pd.value_counts).fillna(0).T

            Maybe   No    Yes
Question1 2.0 2.0 0.0
Question2 2.0 0.0 2.0
Question3 1.0 1.0 2.0

关于python - 使用 pandas 总结分类问卷数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51128057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com