Python Forum
Adding another condition for df column comparison
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding another condition for df column comparison
#1
Hi all,

Here's some code:

print('Total number of changes in DTE value is {}'.format((df['DTE'] != df['DTE'].shift(1)).sum()))
print('Total number of changes in DTE value and DTE < 151 is {}'.format((df['DTE'] != df['DTE'].shift(1)) \
                                                                        and (df['DTE']<151).sum()))
Line 1 correctly prints out the sum, but line 2 gives a ValueError about the series being ambiguous. My intent was just to add a second condition. If the syntax works for one, then why not for both?

Mark
Reply
#2
I think you want this:
(df['DTE'] != df['DTE'].shift(1) & df['DTE'] < 151)
In pandas you use & instead of "and", | instead of "or" and ~ instead of "not"

Your code is hard to read because the lines are long. I would split the calculation from the print
value = (df['DTE'] != df['DTE'].shift(1) & df['DTE'] < 151).sum()
print(f'Total number of changes in DTE value and DTE < 151 is {value}')
Reply
#3
(Sep-30-2021, 03:35 PM)deanhystad Wrote: I think you want this:
(df['DTE'] != df['DTE'].shift(1) & df['DTE'] < 151)
In pandas you use & instead of "and", | instead of "or" and ~ instead of "not"

Your code is hard to read because the lines are long. I would split the calculation from the print
value = (df['DTE'] != df['DTE'].shift(1) & df['DTE'] < 151).sum()
print(f'Total number of changes in DTE value and DTE < 151 is {value}')

That gives me: TypeError: unsupported operand type(s) for &: 'float' and 'float'. Wrapping each of the individual & arguments in parentheses works:

print('Total number of changes in DTE value is {}'.format((df['DTE'] != df['DTE'].shift(1)).sum()))
value = ((df['DTE'] != df['DTE'].shift(1)) & (df['DTE'] < 151)).sum()
print(f'Total number of changes in DTE value and DTE < 151 is {value}')
Thanks for your help!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 319 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 285 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 740 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  create new column based on condition arvin 12 2,250 Dec-13-2022, 04:53 PM
Last Post: jefsummers
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 843 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Adding a new column to a dataframe lokhtar 2 3,164 Jan-14-2021, 07:18 PM
Last Post: buran
  Pandas, Assign a value in a row, to another column based on a condition klllmmm 6 5,507 Oct-16-2020, 04:43 PM
Last Post: klllmmm
  else condition not called when if condition is false Sandz1286 10 5,890 Jun-05-2020, 05:01 PM
Last Post: ebolisa
  [HELP] Nested conditional? double condition followed by another condition. penahuse 25 7,955 Jun-01-2020, 06:00 PM
Last Post: penahuse
  Substring and If then Condition to create column Chandan 2 2,361 Jan-23-2020, 08:40 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020