I am reading a csv file and writing its contents to a dataframe.
df1 = pd.read_csv(r'/C:/Data.csv', sep=',', names=["a", "b", "c"])
I am later comparing the data in one column to a column in a different dataframe. The code fails to execute showing the error '('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', u'occurred at index 0')'.
The have tried using astype method in different variations and nothing has worked so far. Pasting below a piece of code that I have tried that also did not work.
if (df3[df3['c'] == 'Science']):
Appreciate if anyone can help me understand the problem to get it resolved. Thanks in advance.
Adding the code below for better understanding...
df1 = --reading from local(id, student name, class columns)
df2 = --creating this to save the output from the json method(id, subject, marks)
url = --endpoint
def passfail_logic(row):
if ((df3['subject'] == 'viva' & df3['marks'] > 85 ) & (df3['subject'] == 'science' & df3['marks'] > 75) & (df3['subject'] == 'math' & df3['marks'] > 85) ):
res = 'pass'
elif ((df3['subject'] == 'viva' & df3['marks'] > 50 ) & (df3['subject'] == 'science' & df3['marks'] > 45) & (df3['subject'] == 'math' & df3['marks'] > 55)) :
res = 'reattempt'
else:
res = 'fail'
return res
testdata = open('/c:/TestData.csv')
testdataReader = csv.reader(testdata)
testdatalist = list(testdataReader)
for row in testdatalist:
j = "{'id':" + row[0] + ",'student_name':" + row[1] + ",'class':'" + row[2] + "'}"
j = j.replace("'",'"')
j = ast.literal_eval(j)
response = requests.post(url, json=j)
df2 = df2.append(response.json(), ignore_index=True)
df4 = pd.concat([df1, df2], axis=1)
df3['result'] = df3.apply(lambda row : passfail_logic(row ), axis = 1)
df3.to_csv('C:/StudentResult.csv', index=False)
EDIT: here's what's wrong after you updated:
def passfail_logic(row):
if ((df3['subject'] == 'viva' & df3['marks'] ...
df3
should be row
, the &
should be and
I don't think you want the bitwise operator here. I think this is why you keep seeing something about converting to string, 'viva' & df3['marks']
this gets priority in the evaluation and it doesn't know how to compare string ('viva'
) to the series (df3['marks']
). You could wrap these in parenthesis like (df3['subject]=='viva') & (df3['marks']=='...')
but you can also just change to and
What df3[df3['c'] == 'Science']
is doing is basically filtering your dataframe to any rows where column 'c' is 'Science'. So what you end up with is something like if([filtered dataframe]) and it doesn't know how to check the truthfulness of that statement.
It's hard for me to give more information on what to do though because I don't know what you're trying to do with the 'Science' data.
If you just want to grab the filtered data then you could do something like
filtered_df = df3[df3['c'] == 'Science']
if you wanted to apply a function where that column is science you could do something like
def some_function(value):
if value == 'Science':
return 1
else:
return 0
df3['science'] = df3['c'].apply(some_function)