Reputation: 433
I have a dataframe:
df = pd.DataFrame({'a':[1,2,3,4,5,6,7,8,9,10],'b':[100,100,100,100,100,100,100,100,100,100]})
a b
0 1 100
1 2 100
2 3 100
3 4 100
4 5 100
5 6 100
6 7 100
7 8 100
8 9 100
9 10 100
I want to create a column c
, such that for every chunk of 3 rows it assigns a value.
The output i'm looking for:
a b c
0 1 100 1
1 2 100 1
2 3 100 1
3 4 100 2
4 5 100 2
5 6 100 2
6 7 100 3
7 8 100 3
8 9 100 3
9 10 100 4
I tried to iterate through the dataframe and then using .loc to assign column values.
Is there any better/quick way of doing this?
Upvotes: 0
Views: 386
Reputation: 703
If your index is a RangeIndex you can use it to create your values for your c column:
df['c'] = df.index // 3 + 1
Upvotes: 1