简体   繁体   English

Python数据框:基于另一列创建列

[英]Python Dataframe: Create columns based on another column

I have a dataframe with repeated values for one column (here column 'A') and I want to convert this dataframe so that new columns are formed based on values of column 'A'. 我有一个数据框,该数据框的一列(此处为“ A”列)具有重复的值,我想转换此数据框,以便根据列“ A”的值形成新的列。

Example

df = pd.DataFrame({'A':range(4)*3, 'B':range(12),'C':range(12,24)})
df
    A   B   C
0   0   0  12
1   1   1  13
2   2   2  14
3   3   3  15
4   0   4  16
5   1   5  17
6   2   6  18
7   3   7  19
8   0   8  20
9   1   9  21
10  2  10  22
11  3  11  23

Note that the values of "A" column are repeated 3 times. 请注意,“ A”列的值重复3次。

Now I want the simplest solution to convert it to another dataframe with this configuration (please ignore the naming of the columns, it is used for description purpose only, they could be anything): 现在,我想用此配置将最简单的解决方案转换为另一个数据框(请忽略列的命名,它仅用于描述目的,它们可以是任何东西):

        B               C
    A0  A1  A2  A3  A0  A1  A2  A3
0   0   1   2   3   12  13  14  15
1   4   5   6   7   16  17  18  19
2   8   9   10  11  20  21  22  23

You may need assign the group help key by cumcount , then just do unstack 您可能需要按cumcount assign组帮助键,然后才进行unstack

yourdf=df.assign(D=df.groupby('A').cumcount(),A='A'+df.A.astype(str)).set_index(['D','A']).unstack()
   B              C            
A A0 A1  A2  A3  A0  A1  A2  A3
D                              
0  0  1   2   3  12  13  14  15
1  4  5   6   7  16  17  18  19
2  8  9  10  11  20  21  22  23

This is a pivot problem, so use 这是一个pivot的问题,所以在使用

df.assign(idx=df.groupby('A').cumcount()).pivot('idx', 'A', ['B', 'C'])

     B              C            
A    0  1   2   3   0   1   2   3
idx                              
0    0  1   2   3  12  13  14  15
1    4  5   6   7  16  17  18  19
2    8  9  10  11  20  21  22  23

If the headers are important, you can use MultiIndex.set_levels to fix them. 如果标题很重要,则可以使用MultiIndex.set_levels进行修复。

u = df.assign(idx=df.groupby('A').cumcount()).pivot('idx', 'A', ['B', 'C'])
u.columns = u.columns.set_levels(
    ['A' + u.columns.levels[1].astype(str)], level=[1])
u

     B              C            
A   A0 A1  A2  A3  A0  A1  A2  A3
idx                              
0    0  1   2   3  12  13  14  15
1    4  5   6   7  16  17  18  19
2    8  9  10  11  20  21  22  23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据另一个 dataframe 中的列值创建 dataframe 列 - Create a dataframe column based on values that are columns in another dataframe 根据来自另一个数据帧的多列条件创建多列 - Create multiple columns based on multiple column conditions from another dataframe 创建基于另一个列值增加计数的 Dataframe 列 - Create Dataframe column that increases count based on another columns value 遍历列 pandas dataframe 并根据条件创建另一列 - iterate through columns pandas dataframe and create another column based on a condition 根据列和另一个字典中的索引列表创建新的 dataframe 列 - Create new dataframe columns based on lists of indices in a column and another dictionary python dataframe 基于另一列创建一列 - python dataframe create one column based on another column 如何根据python中现有列中的条件创建新列? - How to create a new column based on conditions in the existing columns in a dataframe in python? Python-根据数据框中的其他多个列创建一个列 - Python - Create a column based on multiple other columns in a dataframe 根据多个其他列的条件新建 Python DataFrame 列 - Create new Python DataFrame column based on conditions of multiple other columns Python Pandas Dataframe将列创建为另一列中出现的字符串数 - Python Pandas Dataframe create column as number of occurrence of string in another columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM