I have a df as below and need to print a string on the basis of the condition as follows:
Name Age University City
0 Ankit 23 BHU Pune
1 Aishwarya 21 BHU Pune
2 Shaurya 22 BHU Pune
3 Shivangi 21 BHU Pune
for i in df:
if df['Age']=='BHU'and df['City']=='Pune':
print('correct')
else:
print('error')
gives ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
.
CodePudding user response:
You can looping by array created in numpy.where
:
arr = np.where((df['Age']=='BHU') & (df['City']=='Pune'), 'correct', 'error')
for val in arr:
print(val)
Your solution is possible use:
for row in df.itertuples():
if (row['Age']=='BHU') and (row['City']=='Pune'):
print('correct')
else:
print('error')
CodePudding user response:
I think you are comparing University and City name. If University is "BHU" and city is "Pune" then you want to print("correct") otherwise you want to print error. If my thought is right then you should follow this code below.
for i in range(0,len(df)):
if df.loc[i, "University"] == "BHU" and df.loc[i, "City"] == "Pune":
print("correct")
else:
print("error")