I would like to know how to add multiple constant values of different lengths into a dataframe column. I know that we can add a single constant value (for example: 5) to a data frame column 'A' like this:
df['A'] = 5
But I want to have the dataframe something like the table below. As you can see, I need three 5s, two 10s, six 30s and one 100s. How can you do that for maybe 10000 rows with a set number of values (not random) each having a user defined frequency.
| index | A |
|---|---|
| 1 | 5 |
| 2 | 5 |
| 3 | 5 |
| 4 | 10 |
| 5 | 10 |
| 6 | 30 |
| 7 | 30 |
| 8 | 30 |
| 9 | 30 |
| 10 | 30 |
| 11 | 30 |
| 12 | 100 |
You can use numpy.repeat with the DataFrame constructor:
vals = [5,10,30,100]
reps = [3,2,6,1]
df = pd.DataFrame({'A': np.repeat(vals, reps)})
df.index+=1
output:
A
1 5
2 5
3 5
4 10
5 10
6 30
7 30
8 30
9 30
10 30
11 30
12 100
IIUC you could just use:
df['b'] = np.repeat([5, 5, 5, 10, 10, 30, 30, 30, 30, 30, 30, 100], np.ceil(len(df) / 12))[:len(df)]
Or:
df['b'] = np.repeat([*[5] * 3, *[10] * 2, *[30] * 6, 100], np.ceil(len(df) / 12))[:len(df)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With