Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to increase the value of a new column after 'N' number of rows

I have a Dataframe which consists a thousand rows, and I want to add 1 to particular column "Level" after every 100 rows.

I am sharing a small self created sample of how the data looks like:

import pandas as pd

data  = {'displayName':['Kelsier', 'Val', 'Elend', 'Dock','Club','Renox','Sher','Breeze','Saze','Reen','Mist','Atium','Iron','Copper','Tin'],
        'pic' : ["av10", "av12", "av14", "av15", "av0", "av1", "av18", "av8", "av16", "av22", "av40", "av24", "av17", "av2", "av7"]}
        
df = pd.DataFrame(data)

So for example I want the level to be increased by 1 after every 5 rows, and suppose the value needs to start form 6.
As shown below:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried loops but I am not very good at it currently

import pandas as pd

data  = {'displayName':['Kelsier', 'Val', 'Elend', 'Dock','Club','Renox','Sher','Breeze','Saze','Reen','Mist','Atium','Iron','Copper','Tin'],
        'pic' : ["av10", "av12", "av14", "av15", "av0", "av1", "av18", "av8", "av16", "av22", "av40", "av24", "av17", "av2", "av7"],
        'level':[6,6,6,6,6,7,7,7,7,7,8,8,8,8,8]}
        
df = pd.DataFrame(data)

That’s all. Thanks for your kind help!

>Solution :

Use np.arange with floor division by 5:

df['level'] += np.arange(len(df))//5

Or using a default start value:

df['level'] = np.arange(len(df))//5 + 6

Output:

   displayName   pic  level
0      Kelsier  av10      6
1          Val  av12      6
2        Elend  av14      6
3         Dock  av15      6
4         Club   av0      6
5        Renox   av1      7
6         Sher  av18      7
7       Breeze   av8      7
8         Saze  av16      7
9         Reen  av22      7
10        Mist  av40      8
11       Atium  av24      8
12        Iron  av17      8
13      Copper   av2      8
14         Tin   av7      8
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading