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:
JavaScript
x
2
1
df['A'] = 5
2
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 |
Advertisement
Answer
You can use numpy.repeat
with the DataFrame constructor:
JavaScript
1
5
1
vals = [5,10,30,100]
2
reps = [3,2,6,1]
3
df = pd.DataFrame({'A': np.repeat(vals, reps)})
4
df.index+=1
5
output:
JavaScript
1
14
14
1
A
2
1 5
3
2 5
4
3 5
5
4 10
6
5 10
7
6 30
8
7 30
9
8 30
10
9 30
11
10 30
12
11 30
13
12 100
14