Issue
So I have a dataframe with multiple columns with numbers in them. It looks like this:
H | C | T | P | R |
---|---|---|---|---|
300 | 200 | 500 | 0.3 | |
500 | 400 | 300 | 0.2 |
I'm trying to perform operations on columns H, C, T, P and fill in column R.
For example,
df['R'] = numpy.where(df['H'] > df['T'] and df['P'] > 0,
df['C'] / df['T'] - 1, 0)
I would like the operation to be performed row by row
- when the value of 'H' in nth row > the value of 'T' in nth row
- when the value in 'P' in nth row is greater than 0
But if I run the code, I get "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."
What do I need to fix? Perhaps I need to use row['column name']? Any help is appreciated!
Solution
You should use bitwise &
and parantheses, rather than and
.
df['R'] = numpy.where((df['H'] > df['T']) & (df['P'] > 0),
df['C'] / df['T'] - 1, 0)
Answered By - Collin Cunningham
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.