You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于单列实现关联列透视?并按单列筛选行(附数据示例)

Pandas Solutions for Your Data Processing Needs

Alright, let's break down how to handle both your requests using Python's Pandas library—perfect for tabular data manipulation like this. First, let's get our sample dataset into a DataFrame so we can work with it:

import pandas as pd

# Create the sample dataset
data = [
    [1, 'x1', 'y1', 'Manager', 'x2', 'y2', 1, 'Role1'],
    [1, 'x1', 'y1', 'User', 'x3', 'y3', 0, 'Role2'],
    [2, 'x4', 'y4', 'Admin', 'x2', 'y2', 0, 'Role1'],
    [2, 'x4', 'y4', 'User', 'x6', 'y6', 0, 'Role2'],
    [2, 'x4', 'y4', 'Manager', 'x7', 'y7', 0, 'Role3'],
    [3, 'b1', 'd1', None, None, None, None, None]
]

columns = [
    'PersonID', 'PersonDep', 'PersonBranch', 
    'RoleName', 'RoleDep', 'RoleBranch', 'IsPriority', 'RoleLevel'
]

df = pd.DataFrame(data, columns=columns)

1. Pivot Associated Columns Based on RoleLevel

It looks like you want to transform the data into a wide format where each RoleLevel (like Role1, Role2) has its own set of columns for the role-related fields (RoleName, RoleDep, etc.). Here's how to do that with pivot:

# Pivot the data: use person identifiers as index, RoleLevel as columns, and role fields as values
pivoted_df = df.pivot(
    index=['PersonID', 'PersonDep', 'PersonBranch'],
    columns='RoleLevel',
    values=['RoleName', 'RoleDep', 'RoleBranch', 'IsPriority']
)

# Flatten the multi-level column names for readability
pivoted_df.columns = ['_'.join(col).strip() for col in pivoted_df.columns.values]

# Reset index to make person columns regular columns again
pivoted_df = pivoted_df.reset_index()

Result of the Pivot:

PersonIDPersonDepPersonBranchRoleName_Role1RoleName_Role2RoleName_Role3RoleDep_Role1RoleDep_Role2RoleDep_Role3RoleBranch_Role1RoleBranch_Role2RoleBranch_Role3IsPriority_Role1IsPriority_Role2IsPriority_Role3
1x1y1ManagerUserNaNx2x3NaNy2y3NaN10NaN
2x4y4AdminUserManagerx2x6x7y2y6y7000
3b1d1NaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaNNaN

This gives you the wide-format table you described, with each role's attributes grouped under its RoleLevel label.


2. Filter Rows Based on a Column Value

Filtering rows is straightforward in Pandas—you just create a boolean mask based on your column condition and apply it to the DataFrame. Here are common examples using your dataset:

Example 1: Filter rows where IsPriority is 1

priority_rows = df[df['IsPriority'] == 1]

Result:

PersonIDPersonDepPersonBranchRoleNameRoleDepRoleBranchIsPriorityRoleLevel
1x1y1Managerx2y21Role1

Example 2: Filter rows where RoleName is not null (exclude the empty role row)

non_null_roles = df[df['RoleName'].notna()]

Example 3: Filter rows for a specific PersonID (e.g., PersonID=2)

person_2_rows = df[df['PersonID'] == 2]

Example 4: Filter rows where RoleLevel is 'Role1'

role1_rows = df[df['RoleLevel'] == 'Role1']

You can also combine conditions using & (AND) or | (OR) if needed—just wrap each condition in parentheses. For example:

# Filter rows where IsPriority=1 AND PersonDep='x1'
filtered = df[(df['IsPriority'] == 1) & (df['PersonDep'] == 'x1')]

内容的提问来源于stack exchange,提问作者mhsankar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:41:22