ValueError: ('The truth value of a Series is ambiguous after applying if/else condition in Pandas data frames
I want to create a new variable for the dataframe details
called lower
after iterating through multiple data frames
.
list1
is a list of string values of a column namedvariable_name
in thedetails
.vars_df
is another data frame with 2 columns, namelyvariable_name
anddirection
. Both columns contain with string values.vars_df.shape = (19,2)
Some values ofvariable_name
invars_df
are present inlist1
as well asdata_set
.data_set.shape = (32,107)
.df.shape = (96,1)
The following code, which aims to do the above:
def get_value(df,list1):
if ((vars_df['variable_name']) in list1) (vars_df['direction'] =='up'):
data = data_set[df['variable_name']]
else:
data = data_set[df['variable_name']]-20
if data 0:
data = 0
else:
data = data
return data
details['lower'] = details.apply(get_value,list1=li,result_type='expand',axis=1)
produces this error:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index 0')
I referred the question.Truth value of a series is ambiguous Still I can not rectify the error
Topic dataframe preprocessing pandas python
Category Data Science