python - 统计 pandas 中多列问题的李克特量表结果数

标签 python pandas numpy group-by pandas-groupby

我有以下数据框:

       Question1        Question2         Question3          Question4
User1  Agree            Agree          Disagree         Strongly Disagree
User2  Disagree         Agree          Agree            Disagree
User3  Agree            Agree          Agree            Agree

有没有办法将上面列出的数据框转换为以下内容?

              Agree         Disagree         Strongly Disagree
 Question1    2               1                  0

 Question2    2               1                  0

 Question3    2               1                  0
 Question4    1               1                  1

这与我之前的问题类似:Make a dataframe with grouped questions from three columns

我试着用 stack/pivot 查看以前的问题,但无法弄清楚。实际的数据框有 20 多个问题和李克特量表,从非常同意、同意、中立、不同意、非常不同意。

最佳答案

使用pd.get_dummies

pd.get_dummies(df.stack()).groupby(level=1).sum()

           Agree  Disagree  Strongly Disagree
Question1      2         1                  0
Question2      3         0                  0
Question3      2         1                  0
Question4      1         1                  1

将其提升到另一个层次
我们可以使用 numpy.bincount 来加快速度。但是我们要注意维度

v = df.values
f, u = pd.factorize(v.ravel())
n, m = u.size, v.shape[1]
r = np.tile(np.arange(m), n)
b0 = np.bincount(r * n + f)
pad = np.zeros(n * m - b0.size, dtype=int)
b = np.append(b0, pad)

pd.DataFrame(b.reshape(m, n), df.columns, u)

           Agree  Disagree  Strongly Disagree
Question1      2         1                  0
Question2      3         0                  0
Question3      2         1                  0
Question4      1         1                  1

另一个numpy选项

v = df.values
n, m = v.shape
f, u = pd.factorize(v.ravel())

pd.DataFrame(
    np.eye(u.size, dtype=int)[f].reshape(n, m, -1).sum(0),
    df.columns, u
)

           Agree  Disagree  Strongly Disagree
Question1      2         1                  0
Question2      3         0                  0
Question3      2         1                  0
Question4      1         1                  1

速度差

%%timeit
v = df.values
f, u = pd.factorize(v.ravel())
n, m = u.size, v.shape[1]
r = np.tile(np.arange(m), n)
b0 = np.bincount(r * n + f)
pad = np.zeros(n * m - b0.size, dtype=int)
b = np.append(b0, pad)
​
pd.DataFrame(b.reshape(m, n), df.columns, u)
1000 loops, best of 3: 194 µs per loop

%%timeit
v = df.values
n, m = v.shape
f, u = pd.factorize(v.ravel())

pd.DataFrame(
    np.eye(u.size, dtype=int)[f].reshape(n, m, -1).sum(0),
    df.columns, u
)
1000 loops, best of 3: 195 µs per loop

%timeit pd.get_dummies(df.stack()).groupby(level=1).sum()
1000 loops, best of 3: 1.2 ms per loop

关于python - 统计 pandas 中多列问题的李克特量表结果数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44400661/

相关文章:

python - 将 1300 个数据帧合并为一个帧变得非常慢

python - 如何将多个 .npy 文件加载到单个 numpy 数组中

python - fmin_l_bfgs_b 返回 NaN 作为函数值,但我不明白

python - 如何在django中从自身获取模型名称

Python OpenCV - VideoCapture.release() 在 Linux 中不起作用

python - 如何在 torchvision.transforms 中找到 Normalize 的均值和 STD 的最佳值

python - np.where np.nan 的处理(NaN 评估为值 < 0)

python - 使用 OpenCV 图像提供 Inception

python - 标签编码器 : TypeError: '>' not supported between instances of 'float' and 'str'

python - 迭代两个数据帧的行