jeudi 26 décembre 2019

select the Rows which contains same text in last two columns of data frame in python pandas

I have a data frame which looks below

import pandas as pd

k={'ID':[1,2,3,4,5,6],'m1':['jj','nn','jj','nn','nn','nn'],
   'm2':['jj','nn','nn','jj','jj','jj'],
   'm3':['jj','','nn','jj','jj','jj'],
   'm4':['nn','','nn','jj','jj','jj'],
   'm5':['nn','','','jj','jj','nn'],
   'm6':['','','','jj','jj','nn']}

df=pd.DataFrame(data=k)

ID  m1  m2  m3  m4  m5  m6
1   jj  jj  jj  nn  nn  
2   nn  nn              
3   jj  nn  nn  nn      
4   nn  jj  jj  jj  jj  jj
5   nn  jj  jj  jj  jj  jj
6   nn  jj  jj  jj  nn  nn

we have to select the ID which contains 'nn' in last two columns but the last column for each row(ID) is different

I want the result as follows

ID  last1   last2   last two columns are nn
1   nn       nn         yes
2   nn       nn         yes
3   nn       nn         yes
6   nn       nn         yes 

Aucun commentaire:

Enregistrer un commentaire