-2

I want to iterate over the column "Name" which should have only "ROSE" and the next column should be "Fail" and should extract the column "places" corresponding to the FAIL and ROSE.

INPUT

Output :

OUTPUT

1 Answers1

0
df[(df["Name"] == "Rose") & (df["Condition"] == "Fail")]

Output

   Name   Status  Place
0  Rose     Fail  India
1  Rose     Fail    USA
2  Rose     Fail      X

To fetch only Place column as a list

df[(df["Name"] == "Rose") & (df["Condition"] == "Fail")]["Place"].tolist()

Output

   ['India', 'USA', 'X']
YuserT
  • 16
  • 2
  • The corresponding Places also has to be fetched – Gayathri K Jun 08 '21 at 14:58
  • Do you mean, you would like to have as output a list [India, USA] ? – YuserT Jun 08 '21 at 15:12
  • Based on the condition of ROSE , FAIL ... its corresponding value in the same row in the place column has to be fetched. It can be anything .. x,y.. not just india , usa.. – Gayathri K Jun 08 '21 at 15:16
  • 1
    The code above returns the PLACE column values (which can be anything not just india or usa) for the rows satisfying the (Rose and Fail) condition. – YuserT Jun 08 '21 at 15:19
  • Please accept my answer if it answered your question (https://stackoverflow.com/help/someone-answers) – YuserT Jun 08 '21 at 15:30
  • Hey what if I am getting the Name column from the user. "Rose" should be from user input. all other things are same. @YuserT – Gayathri K Jun 09 '21 at 02:58
  • A code snippet of how you are getting the Name column from the user would be useful. – YuserT Jun 09 '21 at 08:13