如何透视调查/问卷数据并计算多选项问题?
我正在尝试开展一项调查,其中有许多问题都具有相同的水平。 基本上我想从这个:
Customer | Atribute 1 | Atribute 2 | Atribute 3 |
---|---|---|---|
1 | A | B | A |
2 | B | B | A |
3 | C | B | C |
转向:
Product | Atribute 1 | Atribute 2 | Atribute 3 |
---|---|---|---|
A | 1 | 0 | 2 |
B | 1 | 3 | 0 |
C | 1 | 0 | 1 |
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:
Customer | Atribute 1 | Atribute 2 | Atribute 3 |
---|---|---|---|
1 | A | B | A |
2 | B | B | A |
3 | C | B | C |
To this:
Product | Atribute 1 | Atribute 2 | Atribute 3 |
---|---|---|---|
A | 1 | 0 | 2 |
B | 1 | 3 | 0 |
C | 1 | 0 | 1 |
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 技术交流群。

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

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