Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple columns into one category column using the column names as value label

Tags:

python

pandas

I have this data

   ID      A      B      C
0   0   True  False  False
1   1  False   True  False
2   2  False  False   True

And want to transform it into

   ID group
0   0     A
1   1     B
2   2     C
  • I want to use the column names as value labels for the category column.
  • There is a maximum of only one True value per row.

This is the MWE

#!/usr/bin/env python3
import pandas as pd

df = pd.DataFrame({
    'ID': range(3),
    'A': [True, False, False],
    'B': [False, True, False],
    'C': [False, False, True]
})

result = pd.DataFrame({
    'ID': range(3),
    'group': ['A', 'B', 'C']
})
result.group = result.group.astype('category')

print(df)
print(result)

I could do df.apply(lambda row: ...magic.., axis=1). But isn't there a more elegant way with pandas' own tools?

like image 733
buhtz Avatar asked Sep 09 '25 17:09

buhtz


1 Answers

You can use df.dot:

df['group'] = df[['A', 'B', 'C']].dot(df.columns[1:])
like image 71
Nuri Taş Avatar answered Sep 12 '25 13:09

Nuri Taş