Skip to content Skip to sidebar Skip to footer

How To Create New Columns Depending On Row Value In 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

Solution 1:

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
time0.251010450.501010670.751010891.00101011121.25111113141.50111115161.75111117182.0011111920

If you want to remove the index name:

piv.rename_axis(None, inplace=True)

      spk_1_label_1  spk_2_label_1  spk_1_label_2  spk_2_label_2
0.251010450.501010670.751010891.00101011121.25111113141.50111115161.75111117182.0011111920

Extra

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.251010450.501010670.751010891.00101011121.25111113141.50111115161.75111117182.0011111920

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]

Post a Comment for "How To Create New Columns Depending On Row Value In Pandas"