Search For A Value Anywhere In A Pandas Dataframe
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 search for a value som
Solution 1:
You can perform equality comparison on the entire DataFrame:
df[df.eq(var1).any(1)]
Solution 2:
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
Solution 3:
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 inrange(len(z)-1):
l = x[x[z[k]].str.match(keyWord)]
print(l.head(10))
k = k+1except:
print("")
Post a Comment for "Search For A Value Anywhere In A Pandas Dataframe"