def director_release_year(director_df,filter_column,director_name):
if filter_column not in director_df.columns:
raise Exception("the column is not available in the DataFrame")
# Tests that the the column value is in the dataframe
if director_name in director_df[filter_column].tolist()== False:
raise ValueError("the column don't have the value")
#filter the dataframe based on the given director_name
df=director_df[director_df[filter_column]==director_name]
# return the result
return df
if i give the unavailable value for director_name the output is a empty df instead of this exception error
movie_title release_date hero villian song name director release_year
I tried the following assert , but the result is one or other error not the exact assert i set for
assert director_name in director_df[director_name].values,"The name does not exist in the director column"
o/p:
KeyError: 'siva'
assert director_df[filter_column].isin([director_name]),"The name does not exist in the director column"
o/p:
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
CodePudding user response:
I think you can write
if (director_df[filter_column] != director_name).all():
raise ValueError("the column don't have the value")
The condition above basically checks if all values in director_df[filter_column]
does not equal director_name
.
CodePudding user response:
As you are checking a string director_name
in a dataframe director_df
, it results in a series of bool values. So its better to check for conditionality using .any()
or .all()
. This concept is clearly explained in this Reference link.
if not (director_df['filter_column'] == director_name).any():
raise ValueError