Search code examples
pythonpandasopenpyxlxlsxxlrd

How to get the value if the value is present in the xlsx sheet without knowing index number?


I have a unstructured Xslx file. I want to get the full row if the values are present in the sheet. For example

A     B    C    D   F  

abc  10   24   32   54
cdf  9    10   34   98
mgl  11   90   21   98
fgd  1    9     2   10

I want to get if the 10 value present in the sheet to get the full row values

output =>


abc  10   24   32   54

cdf  9    10   34   98

fgd  1    9     2   10

thanks for the contributions


Solution

  • Use DataFrame.eq with DataFrame.any for test if at least one True per rows:

    df = pd.read_excel('file.xlsx')
    
    df1 = df[df.eq(10).any(axis=1)]
    

    Or:

    df1 = df[(df == 10).any(axis=1)]
    
    print (df1)
         A   B   C   D   F
    0  abc  10  24  32  54
    1  cdf   9  10  34  98
    3  fgd   1   9   2  10