[Python] How can I find a value in a row, another value in the next row and show row if it...

Discussão em 'Python' iniciado por Stack, Fevereiro 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am new to Python. I am trying to find out if any value matches with any row from the data and another given value matches with the next row, then print the first row and continue until the last row

    import pandas as pd
    df= pd.read_csv ("prac.csv")
    n = 0
    a = input ("write the 1st number: ")
    b = input ("write the 2nd number: ")

    while n < 5 :
    if df.loc[n:n] == a:
    if df.loc[n+1:n+1] == b:
    print (df.loc[n:n])
    else: print ("No match")
    n+=1


    [​IMG]

    For example, if the first value (4) matches with row 1 and the second value (22) with row 2, then it should print row 1 and continue.

    [​IMG]

    Continue reading...

Compartilhe esta Página