如何将数据切片并创建引用其他列值的新列中的Python中的Pivot?

发布于 2025-02-11 13:44:04 字数 1550 浏览 3 评论 0原文

我正在进行调查,数据看起来像这样:

ID    Q1    Q2    Q3    Gender    Age    Dep    Ethnicity
001   Y      N    Y      M         22     IT        W
002   N      Y    Y      M         35     HR        W
003   Y      N    N      M         20     IT        A
004   Y      N    Y      M         54     OPRE      B
005   Y      N    Y      M         42     OPRE      B

现在,我想添加群体种族和性别来创建一个表:

Question  Dep   Response  #White Man   #Diverse Man    %White Man  %Diverse Man 
 Q1     IT        Y        1               1           50           50
        IT        N        0               0            0            0
        HR        Y        0               0            0            0
        HR        N        1               0            100          0 
        OPRE      Y        0               2            0          100
        OPRE      N        0               0            0            0  
 Q2     IT        Y        0               0            0            0
        IT        N        1               1           50           50
        HR        Y        1               0            100          0
        HR        N        0               0            0            0 
        OPRE      Y        0               0            0            0
        OPRE      N        0               2            0            0
 Q3 ......

我的代码是这样的:

df['White Man'] = df[df[Gender] == 'Man']&df[Ethnicity] = 'White'] 

但是我不知道是否包含功能是否python。我上面做的只是过滤,它不会在结果中添加1个。 谁能帮忙?

I am working on a survey and the data looks like this:

ID    Q1    Q2    Q3    Gender    Age    Dep    Ethnicity
001   Y      N    Y      M         22     IT        W
002   N      Y    Y      M         35     HR        W
003   Y      N    N      M         20     IT        A
004   Y      N    Y      M         54     OPRE      B
005   Y      N    Y      M         42     OPRE      B

Now, I'd like to add group ethnicity and Gender to create a table like:

Question  Dep   Response  #White Man   #Diverse Man    %White Man  %Diverse Man 
 Q1     IT        Y        1               1           50           50
        IT        N        0               0            0            0
        HR        Y        0               0            0            0
        HR        N        1               0            100          0 
        OPRE      Y        0               2            0          100
        OPRE      N        0               0            0            0  
 Q2     IT        Y        0               0            0            0
        IT        N        1               1           50           50
        HR        Y        1               0            100          0
        HR        N        0               0            0            0 
        OPRE      Y        0               0            0            0
        OPRE      N        0               2            0            0
 Q3 ......

My codes are like this:

df['White Man'] = df[df[Gender] == 'Man']&df[Ethnicity] = 'White'] 

But I don't know if there is a Contains function in Python or not. What I did above is only filtering, it will not add 1 in the results.
Can anyone help?

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

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

发布评论

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

评论(1

谁人与我共长歌 2025-02-18 13:44:04

我不确定这是解决您的问题的最有效方法,但是它至少可以

df = pd.DataFrame({
    'Q1': ['Y', 'N', 'Y', 'Y', 'Y'],
    'Q2': ['N', 'Y', 'N', 'N', 'N'],
    'Q3': ['Y', 'Y', 'N', 'Y', 'Y'],
    'Gender': ['M', 'M', 'M', 'M', 'M'],
    'Age': [22, 35, 20, 54, 42],
    'Dep': ['IT', 'HR', 'IT', 'OPRE', 'OPRE'],
    'Ethnicity': ['W', 'W', 'A', 'B', 'B'],
    })

df = pd.melt(df, id_vars = ['Gender', 'Age', 'Dep', 'Ethnicity'],
       value_vars=['Q1', 'Q2', 'Q3'],
       var_name='Question',
       value_name='Answer')

df = pd.pivot_table(df, index=['Question', 'Dep','Answer', 'Ethnicity'], aggfunc='count')['Age']

df = df.unstack(level=3).reset_index()
df = df.fillna(0)

从这里看到它可以完全解决您想要的东西。

I'm not sure this is the most efficient way of solving your problem, but it solves it as far as I can see

df = pd.DataFrame({
    'Q1': ['Y', 'N', 'Y', 'Y', 'Y'],
    'Q2': ['N', 'Y', 'N', 'N', 'N'],
    'Q3': ['Y', 'Y', 'N', 'Y', 'Y'],
    'Gender': ['M', 'M', 'M', 'M', 'M'],
    'Age': [22, 35, 20, 54, 42],
    'Dep': ['IT', 'HR', 'IT', 'OPRE', 'OPRE'],
    'Ethnicity': ['W', 'W', 'A', 'B', 'B'],
    })

df = pd.melt(df, id_vars = ['Gender', 'Age', 'Dep', 'Ethnicity'],
       value_vars=['Q1', 'Q2', 'Q3'],
       var_name='Question',
       value_name='Answer')

df = pd.pivot_table(df, index=['Question', 'Dep','Answer', 'Ethnicity'], aggfunc='count')['Age']

df = df.unstack(level=3).reset_index()
df = df.fillna(0)

At least from here it should be easy for you to fully reach what you want

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