import pandas as pd
import numpy as np
rng = np.random.default_rng()
df = pd.DataFrame(rng.integers(0, 100, size=(100, 4)), columns=list('ABCD'))
This is my dataframe. I want to create a new column which starts from 1 and increases by 1 for every 10 rows. So, the column will have a value of 1 for the first ten rows, two for rows 11-20, 3 for 21-30... and so on.
CodePudding user response:
You can use numpy
's arange
with floor division by your step and addition of the start:
start = 1
step = 10
df['new'] = np.arange(len(df))//step start
output:
A B C D new
0 6 80 51 21 1
1 74 52 18 24 1
2 14 25 19 89 1
3 21 89 2 69 1
4 46 32 77 98 1
.. .. .. .. .. ...
95 62 87 89 65 10
96 88 70 44 68 10
97 71 14 2 10 10
98 45 62 89 65 10
99 62 40 45 93 10
[100 rows x 5 columns]
CodePudding user response:
You can use repeat:
d['new'] = np.repeat(np.arange(1, 11), 10)