I have a variable var = myString
and a list of dataframes dfList
. Each dataframe in the list is two columns of strings df = pd.DataFrame({'col1': ['x','y','z'], 'col2': ['a','myString','b']})
. If var
appears anywhere in any dataframe in the list I need to execute a series of functions. I could check by looping through each dataframe in the list, but I am hoping for a method of checking that I can easily incorporate into part of a one line elif statement.
CodePudding user response:
Starting with this solution, to find if a value is present in a DataFrame, we can extend it to a list of dataframes as:
True in [df.isin([var]).any().any() for df in dfList]
Which will be True
if var is present, False
otherwise.
It works whether var is a string or any other type.
CodePudding user response:
Try this.
import pandas as pd
var = 'myString'
df = pd.DataFrame({'col1': ['x','y','z'], 'col2': ['a','myString','b']})
mystringIN = any((df[a] == var).any() for a in df.columns)
print(mystringIN)
Output
True
CodePudding user response:
You can try using list comprehensions as they are significantly faster than looping
import pandas as pd
# Create DataFrame
lst = ['Geeks', 'For', 'Geeks', 'is', 'portal', 'for', 'Geeks']
lst2 = [11, 22, 33, 44, 55, 66, 77]
df1 = pd.DataFrame(list(zip(lst, lst2)),
columns =['Name', 'val'])
df_list = [df1]
# Check if var exists in df
df_var_list = [f'Var Exists!' for df in df_list if 'Geeks' in df.values]
# Out[14]: ['Var Exists']