我有一个数据框,如下所示。
Unit_ID Type Sector Plot_Number Rental
1 Home se1 22 50
2 Shop se1 26 80
从上面我需要写功能来检查如下所示的列列表是否在数据框中。
如果列表为['Unit_ID', 'Sector', 'Usage_Type', 'Price' ]
预期的输出:数据框中没有“ Usage_Type”列和“ Price”列。
如果列表为['Unit_ID', 'Sector' , 'Type', 'Plot_Number' ]
预期输出:列表中的所有同伴都在数据框中
答案 0 :(得分:1)
您可以尝试在下面使用:
#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'
答案 1 :(得分:1)
列名称列表可以通过以下方式找到:
columns = list(my_dataframe)
现在,您可以遍历搜索列表,并检查columns
列表中是否存在每个元素。
def search_func(to_check, columns):
not_present = []
for i in to_check:
if i not in columns:
not_present.append(i)
return not_present
to_check = ['Unit_ID', 'Sector', 'Usage_Type', 'Price' ]
not_present = search_func(to_check, columns)
if len(not_present) == 0:
print(" All coulmns are in the dataframe")
else:
print (not_present, "not present in dataframe")