I have a data frame as shown below.
Unit_ID Type Sector Plot_Number Rental
1 Home se1 22 50
2 Shop se1 26 80
From the above I need write function to check whether the list of columns as shown below is in the data frame.
if list is ['Unit_ID', 'Sector', 'Usage_Type', 'Price' ]
Expected Output: column 'Usage_Type' and 'Price' is/are not in the dataframe.
if list is ['Unit_ID', 'Sector' , 'Type', 'Plot_Number' ]
Exepected Output: All coulmns in the list are in the dataframe
You can try using below:
#For checking if the list of columns are actually
#a subset of the dataframe columns or not , you can use:
def myf1(x,to_check):
if not set(to_check).issubset(set(x.columns)):
return f"{' and '.join(set(to_check).difference(x.columns))} are not available in the dataframe"
return "All columns are available in the dataframe"
to_check = ['Unit_ID', 'Sector']
myf1(df,to_check)
#'All columns are available in the dataframe'
to_check = ['Unit_ID', 'Sector','XYZ']
myf1(df,to_check)
#'XYZ are not available in the dataframe'