【问题标题】:How to create new columns depending on row value in pandas如何根据熊猫中的行值创建新列
【发布时间】:2019-07-03 20:46:10
【问题描述】:

我有一个如下所示的数据框:

    time  speaker  label_1  label_2
0   0.25        1       10        4
1   0.25        2       10        5
2   0.50        1       10        6
3   0.50        2       10        7
4   0.75        1       10        8
5   0.75        2       10        9
6   1.00        1       10       11
7   1.00        2       10       12
8   1.25        1       11       13
9   1.25        2       11       14
10  1.50        1       11       15
11  1.50        2       11       16
12  1.75        1       11       17
13  1.75        2       11       18
14  2.00        1       11       19
15  2.00        2       11       20

'speaker' 列产生 1 和 2 以在给定时间戳描绘 2 个发言者。我想从仅与一位发言者关联的“label_1”和“label_2”数据中创建新列。有关所需的输出,请参见下文。

 time  spk_1_label_1  spk_2_label1  spk_1_label_2  spk_2_label_2
   0.25        10         10             4               5
   0.50        10         10             6               7
   0.75        10         10             8               9
   1.00        10         10            11               12    
   1.25        11         11            13               14
   1.50        11         11            15               16
   1.75        11         11            17               18
   2.00        11         11            19               20

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    首先,我们使用pivot_table 将行转为列。然后我们通过字符串连接list_comprehensionf-string来创建我们想要的列名:

    piv = df.pivot_table(index='time', columns='speaker')
    piv.columns = [f'spk_{col[1]}_{col[0]}' for col in piv.columns]
    
          spk_1_label_1  spk_2_label_1  spk_1_label_2  spk_2_label_2
    time                                                            
    0.25             10             10              4              5
    0.50             10             10              6              7
    0.75             10             10              8              9
    1.00             10             10             11             12
    1.25             11             11             13             14
    1.50             11             11             15             16
    1.75             11             11             17             18
    2.00             11             11             19             20
    

    如果要删除索引名称:

    piv.rename_axis(None, inplace=True)
    
          spk_1_label_1  spk_2_label_1  spk_1_label_2  spk_2_label_2
    0.25             10             10              4              5
    0.50             10             10              6              7
    0.75             10             10              8              9
    1.00             10             10             11             12
    1.25             11             11             13             14
    1.50             11             11             15             16
    1.75             11             11             17             18
    2.00             11             11             19             20
    

    额外

    如果您愿意,我们可以通过使用列名作为扁平列的前缀来使其更通用:

    piv.columns = [f'{piv.columns.names[1]}_{col[1]}_{col[0]}' for col in piv.columns]
    
          speaker_1_label_1  speaker_2_label_1  speaker_1_label_2  speaker_2_label_2
    time                                                                            
    0.25                 10                 10                  4                  5
    0.50                 10                 10                  6                  7
    0.75                 10                 10                  8                  9
    1.00                 10                 10                 11                 12
    1.25                 11                 11                 13                 14
    1.50                 11                 11                 15                 16
    1.75                 11                 11                 17                 18
    2.00                 11                 11                 19                 20
    

    注意:如果您的python版本f-strings,我们可以使用.format进行字符串格式化:

    ['spk_{}_{}'.format(col[0], col[1]) for col in piv.columns]
    

    【讨论】:

    • 啊,我也有同样的答案,晚了 30 秒。
    • 我真的很喜欢多级列重命名。比我过去所做的要好得多。
    • 谢谢,f-strings 对 python 来说是一个非常棒的新特性,特别是如果你可以用它在列表理解中编写如此干净的代码。 @BrendanCox
    • 很高兴我能帮上忙,我添加了一些额外的代码以便我们可以概括它,如果你想要@connor449,你可以试试看:)
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2022-08-09
    • 2023-02-05
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多