Skip to content
Advertisement

Adding multiple constant values in a pandas dataframe column

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

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement