Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a categorical column based on different binary columns in python

I have a dataset that looks like this:

df = pd.DataFrame(data= [[0,0,1],[1,0,0],[0,1,0]], columns = ['A','B','C'])

    A   B   C
0   0   0   1
1   1   0   0
2   0   1   0

I want to create a new column where on each row appears the value of the previous column where there is a 1:

    A   B   C value
0   0   0   1   C
1   1   0   0   A
2   0   1   0   B
like image 427
Rubén Avatar asked Jun 29 '26 04:06

Rubén


2 Answers

Use dot:

df['value'] = df.values.dot(df.columns)

Output:

   A  B  C value
0  0  0  1     C
1  1  0  0     A
2  0  1  0     B
like image 177
Scott Boston Avatar answered Jun 30 '26 17:06

Scott Boston


Using pd.DataFrame.idxmax:

df['value'] = df.idxmax(1)

print(df)

   A  B  C value
0  0  0  1     C
1  1  0  0     A
2  0  1  0     B
like image 42
jpp Avatar answered Jun 30 '26 18:06

jpp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!