'Randomly multiply or divide a pandas column by 2
I have a pandas dataframe with a column with integers that I would like to randomly divide or multiply. The result would look like this:
col_1 new_col
2 1
2 1
4 8
4 2
4 8
4 8
Solution 1:[1]
np.random.choice
df['new_col'] = df['col_1'] * np.random.choice([2, 1/2], len(df))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Shubham Sharma |
