2
df['svc_port'] = np.where(
     min(df['sPort'],df['dPort']) <= 1024, 
     min(df['sPort'],df['dPort']), 
     df['dPort']
)

In the above code, min(df['sPort'],df['dPort']) <= 1024 - the same thing is given in Compare two columns using pandas. I am not using any logical operators. just checking a condition and replacing it's values.

Why am I still getting this error?

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Machavity
  • 30,841
  • 27
  • 92
  • 100
Kathiravan Natarajan
  • 3,158
  • 6
  • 22
  • 45

1 Answers1

2

You are looking for an element-wise min. The builtin min function only works for a single iterable, not multiple of them at a time.

What you're looking for is something along these lines, using np.minimum.

v = np.minimum(df['sPort'], df['dPort'])
df['svc_port'] = np.where(v <= 1024, v, df['dPort'])
cs95
  • 379,657
  • 97
  • 704
  • 746