I have a dataframe (a very large one) that looks as follows:
| id | class_number | a_1 | a_2 | a_3 | a_4 |
|---|---|---|---|---|---|
| 0 | 1 | 1 | 0 | 0 | 1 |
| 1 | 1 | 1 | 1 | 0 | 1 |
| 2 | 1 | 1 | 1 | 1 | 1 |
| 3 | 1 | 1 | 0 | 2 | 1 |
| 4 | 1 | 1 | 2 | 0 | 3 |
For the sake of completeness, here is a screenshot containing a larger cutout of this dataframe:
How can we replace all ones (all values 1) within the columns a_1 to a_1000 each with a random value other than 0, 1 and 2?
What I tried so far works but seems not to be elegant:
cols = ["a_" + str(i) for i in range(1, 1000+1)]
for col in cols:
df[col] = df[col].apply(lambda x: random.choice(range(3, 20)) if x == 1 else x)
df.head()
I would be greatful for any hint to implement this in a more staright-forward manner.
Note df[cols].apply(...) does not work, since it yields an error "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

df[cols].apply(lambda x: x.replace({1:random.choice(range(3, 20))}))?df[cols].apply(lambda x: np.where(x==1, random.choice(range(3,20)), x))