如何根据 pandas 中的行值创建新列 [英] How to create new columns depending on row value in pandas

查看:86
本文介绍了如何根据 pandas 中的行值创建新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

I have a dataframe that looks like this:

    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

扬声器"列产生1和2,以在给定时间戳记下描绘2个扬声器.我想从仅与一位发言人相关的'label_1'和'label_2'数据中创建新列.有关所需的输出,请参见下文.

The 'speaker' column yields 1 and 2 to delineate 2 speakers at a given timestamp. I want to make new columns from the 'label_1' and 'label_2' data that are associated with only one speaker. See below for desired output.

 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

推荐答案

首先,我们使用pivot_table将行旋转为列.然后,我们通过将字符串与list_comprehensionf-string串联来创建所需的列名称:

First we use pivot_table to pivot our rows to columns. Then we create our desired column names by string concatenating with list_comprehension and f-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


额外

如果需要,我们可以使用列名作为展平列的前缀来使其更通用:

If you want, we can make it more general by using the column name as prefix for your flattened columns:

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版本< 3.5,您不能使用f-strings,我们可以使用.format进行字符串格式化:


Notice: if your python version < 3.5, you can't use f-strings, we can use .format for our string formatting:

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

这篇关于如何根据 pandas 中的行值创建新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆