简体   繁体   English

转置列时按唯一值分组

[英]Grouping by unique values while transposing column

I asked a similar question the other day with data from two columns: 前几天,我用来自两列的数据问了一个类似的问题:

Grouping columns by unique values in Python 在Python中按唯一值对列进行分组

Now I have three columns. 现在我有三列。 They need to be grouped by column A with column B as the header values and column C sorted properly. 它们需要按A列分组,其中B列作为标题值,C列正确排序。

My data frame looks like: 我的数据框如下所示:

    A   B   C
25115  20  45
25115  30  154
25115  40  87
25115  70  21
25115  90  74
26200  10  48
26200  20  414
26200  40  21
26200  50  288
26200  80  174
26200  90  54

But I need to end up with this: 但是我需要结束这个:

       10   20   30   40   50   70   80   90
25115       45   154  87        21        74
26200  48   414       21   288       174  54

This gets the values of column C, but not with column B as the row names. 这将获取列C的值,但不使用列B作为行名。

import pandas as pd
df = pd.DataFrame({'A':[25115,25115,25115,25115,25115,26200,26200,26200,26200,26200,26200],'B':[20,30,40,70,90,10,20,40,50,80,90],'C':[45,154,87,21,74,48,414,21,288,174,54]})
a = df.groupby('A')['C'].apply(lambda x:' '.join(x.astype(str)))

Any ideas would be most appreciated. 任何想法将不胜感激。

  • Option 1: 选项1:

Use pivot_table: 使用数据透视表:

df.pivot_table(values='C',index='A',columns='B')

Output 产量

B        10     20     30    40     50    70     80    90
A                                                        
25115   NaN   45.0  154.0  87.0    NaN  21.0    NaN  74.0
26200  48.0  414.0    NaN  21.0  288.0   NaN  174.0  54.0
  • Option 2: 选项2:

Use set_index / unstack: 使用set_index / unstack:

df.set_index(['A','B'])['C'].unstack()

Output: 输出:

B        10     20     30    40     50    70     80    90
A                                                        
25115   NaN   45.0  154.0  87.0    NaN  21.0    NaN  74.0
26200  48.0  414.0    NaN  21.0  288.0   NaN  174.0  54.0

暂无
暂无

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

相关问题 将列值转换为行并根据另一列的值将它们分组 - Transposing column values into rows and grouping them based on value of another column 对列中的唯一字符串进行分组并对单独的列值执行 Function - Grouping Unique Strings in a Column and Performing Function On Separate Column Values 按唯一值分组列表 - Grouping list by unique values 计算列中唯一值之间的差异,并按唯一值的数量进行平均,并按 Python 中的另一列进行分组 - Calculating the differences between unique values in the column and averaging by number of unique values with grouping by another column in Python 根据熊猫中列的唯一分组对数据框值求和 - summing dataframe values based on unique grouping of column in pandas Python Panda 按唯一值对一列进行分组,在分组前根据唯一值的总数创建新列 - Python Panda grouping one column by unique values, creating new column based off the total count of the unique values before grouping 将其他子集用作分组变量时,在Pandas DataFrame中转置列的子集? - Transposing a subset of columns in a Pandas DataFrame while using others as grouping variable? 数据框布局调整-换位,将行转换为列名并按 - Dataframe layout adaption - transposing, turning rows into column names and grouping by 对唯一列值进行分组以获得 pandas dataframe 列中每个唯一值的平均值 - Grouping unique column values to get average of each unique value in pandas dataframe column 将唯一列值分组为 pandas dataframe 列中每个唯一值的总和 - Grouping unique column values to sum of each unique value in pandas dataframe column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM