Busy Bee Busy Bee - 27 days ago 10
Python Question

pandas: add new column that increment every several rows

I want to create a new column in pandas that increments every 5 rows containing specific data (column X) like below

1
1
1
1
1
2
2
2
2
2
3

Answer Source

another option:

df['new'] = df.index / 5

uses odd python 2 division to floor your index. (i'm sure there's a way to do it similarly in 3?)

edit:

df['new'] = df.index / 5 + 1

works to give you values from 1 instead of 0

python 3 thanks to BusyBee:

df['new']= (df.index / 5 + 1).astype(int)

If you want to start at row x:

import pandas as pd

df = pd.DataFrame({'data': range(50)}, columns=['data'])

x = 23

df['two'] = None

df.loc[x:, 'two'] = df.index[x:] / 5 +1

print df

if you want to start at x and then number from 1 you need to subtract x

df.loc[x:, 'two'] = (df.index[x:] - x) / 5 +1

but I'm not sure this is the best method for that anymore.

you can use .shift after you apply it but for some reason you can't shift df.index (probably a good reason!)