Search code examples
pythonpandasdataframeconditional-statements

How to counts "Y" in Row?


How to counts "y" value in python and update this output new column.

Example:- col 3 to col 7 find the value "y" and update the counts the last col.

like this image.

enter image description here


Solution

  • if you are looking to calculate all the 'Y' in row-wise level then you can use the below condition:

    
    df['counts']=(df == 'Y').sum(axis=1)
    
    

    if you want to mention specific columns then u can use the below condition:

    df['counts']=(df[['INR','USA','UK','SA','ENG']]=='Y').sum(axis=1)