Search for a value anywhere in a pandas DataFrame

后端 未结 3 835
时光说笑
时光说笑 2020-12-24 03:45

This seems like a simple question, but I couldn\'t find it asked before (this and this are close but the answers aren\'t great).

The question is: if I want to searc

相关标签:
3条回答
  • 2020-12-24 03:53

    You should using isin , this is return the column , is want row check cold' answer :-)

    df.isin(['bal1']).any()
    A        False
    B         True
    C        False
    CLASS    False
    dtype: bool
    

    Or

    df[df.isin(['bal1'])].stack() # level 0 index is row index , level 1 index is columns which contain that value 
    0  B    bal1
    1  B    bal1
    dtype: object
    
    0 讨论(0)
  • 2020-12-24 03:54

    You can try the code below:

    import pandas as pd
    x = pd.read_csv(r"filePath")
    x.columns = x.columns.str.lower().str.replace(' ', '_')
    y = x.columns.values
    z = y.tolist()
    print("Note: It take Case Sensitive Values.")
    keyWord = input("Type a Keyword to Search: ")
    try:
        for k in range(len(z)-1):
            l = x[x[z[k]].str.match(keyWord)]
            print(l.head(10))
            k = k+1
    except:
        print("")
    
    0 讨论(0)
  • 2020-12-24 04:13

    You can perform equality comparison on the entire DataFrame:

    df[df.eq(var1).any(1)]
    

    Another option is using numpy comparison:

    df[(df.values.ravel() == var1).reshape(df.shape).any(1)]
    
    0 讨论(0)
提交回复
热议问题