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

Time:12-20

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:

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!

CodePudding user response:

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

CodePudding user response:

Use integer division by 5 and add to column level - solution if default RangeIndex:

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

df['level'] = df.index // 5   6

Or if not RangeIndex is possible create helper array:

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

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