Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop to extract header for a dataframe in pandas

I am a newbie in python. I have a data frame that looks like this:

    A   B   C   D   E
0   1   0   1   0   1
1   0   1   0   0   1
2   0   1   1   1   0
3   1   0   0   1   0
4   1   0   0   1   1

How can I write a for loop to gather the column names for each row. I expect my result set looks like that:

    A   B   C   D   E   Result
0   1   0   1   0   1   ACE
1   0   1   0   0   1   BE
2   0   1   1   1   0   BCD
3   1   0   0   1   0   AD
4   1   0   0   1   1   ADE

Anyone can help me with that? Thank you!

like image 223
zihan meng Avatar asked Jan 06 '23 10:01

zihan meng


1 Answers

The dot function is done for that purpose as you want the matrix dot product between your matrix and the vector of column names:

df.dot(df.columns)
Out[5]: 
0    ACE
1     BE
2    BCD
3     AD
4    ADE

If your dataframe is numeric, then obtain the boolean matrix first by test your df against 0:

(df!=0).dot(df.columns)

PS: Just assign the result to the new column

df['Result'] = df.dot(df.columns)

df
Out[7]: 
   A  B  C  D  E Result
0  1  0  1  0  1    ACE
1  0  1  0  0  1     BE
2  0  1  1  1  0    BCD
3  1  0  0  1  0     AD
4  1  0  0  1  1    ADE
like image 154
Zeugma Avatar answered Jan 13 '23 10:01

Zeugma