1

I have a code like:

df = pd.DataFrame([{'a': 1, 'b': 2}])
df['c'] = min(5, df['a'] + df['b'])

my goal was to add a column 'c' to be the minimum of a constant number and the sum of column 'a' and 'b'. But Python gives error message on the second line like:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

What does this message mean? And how can I achieve my goal?

Cuteufo
  • 501
  • 6
  • 15
  • 1
    Note that [`min`](https://docs.python.org/3/library/functions.html#min) can't be used this way even without pandas, e.g. `min(5, [3])` is not valid. [`min`](https://docs.python.org/3/library/functions.html#min) accepts either an iterable _or_ individual values, not mixed. Specifically regarding pandas, see the extensive answers in [Truth value of a Series is ambiguous](https://stackoverflow.com/q/36921951/13138364). – tdy Sep 02 '21 at 17:36

2 Answers2

5

You can use clip to set all values below 5 to 5:

df["c"] = df[["a","b"]].sum(axis=1).clip(5)
tdy
  • 36,675
  • 19
  • 86
  • 83
not_speshal
  • 22,093
  • 2
  • 15
  • 30
0

try this

df['c'] = df.apply(lambda x: min(5, sum(x.values)), axis=1)
Ian SHEN
  • 36
  • 3
  • note that `apply(axis=1)` technically works but is very slow (e.g. with a 10K rows, this is ~70x slower than the vectorized `sum`+`clip`) – tdy Sep 02 '21 at 17:44