如何透视调查/问卷数据并计算多选项问题?

发布于 01-09 15:43 字数 842 浏览 2 评论 0原文

我正在尝试开展一项调查,其中有许多问题都具有相同的水平。 基本上我想从这个:

CustomerAtribute 1Atribute 2Atribute 3
1ABA
2BBA
3CBC

转向:

ProductAtribute 1Atribute 2Atribute 3
A102
B130
C101

In我的真实数据有几十个列和级别(A、B、C...Z)和数百个客户。 我曾经能够在 R 中做到这一点,但那是几年前的事了,算法过于复杂。我想知道 Python/pandas 是否有一个简单的解决方案。

I'm trying to pivot a survey with many questions all sharing the same levels.
Basically I want to pivot from this:

CustomerAtribute 1Atribute 2Atribute 3
1ABA
2BBA
3CBC

To this:

ProductAtribute 1Atribute 2Atribute 3
A102
B130
C101

In my real data I have dozens of columns and levels (A, B, C...Z) and hundreds of customers.
I was once able to do this in R but it was years ago with an overcomplicated algorithm. I'm wondering if Python/pandas has an easy fix for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灼疼热情2025-01-16 15:43:40

我假设你的 DataFrame 名为 df,那么你可以这样做:

import pandas as pd

df.apply(pd.Series.value_counts).fillna(0).astype(int).drop("Customer" , axis=1).rename_axis("Product")

这给了我:
输入图片此处描述

I suppose your DataFrame named df, then you can do it in this way:

import pandas as pd

df.apply(pd.Series.value_counts).fillna(0).astype(int).drop("Customer" , axis=1).rename_axis("Product")

this gives me:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文