I have a dataframe and I have written the following function to populate a new column:
df = pd.DataFrame(np.random.randn(10, 2), columns=['a', 'b'])
def perc(a,b):
if a/b < 0:
n = 0
elif a/b > 1:
n = 1
else:
n = a/b
return n
df['c']=perc(df['a'],df['b'])
df[1:10]
It's supposed to calculate a percent column. Here is the error I am getting:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I understand that it has to do with dif and unc being series instead of individual elements. But how do I fix it?