Home > Software engineering >  Pandas expanding a dataframe's length and populating every nth row
Pandas expanding a dataframe's length and populating every nth row

Time:05-05

I have a dataframe with two columns that I'm interested in which looks like this:

    frame   requests
0   0   214388438.0
1   1   194980303.0
2   2   179475934.0
3   3   165196540.0
4   4   154815540.0
5   5   123650671.0
6   6   119089045.0

This goes on for many rows. So what I'm looking for is a way to keep the value on the requests column then add rows filled with 0 and repeat the process for a certain amount of rows. frame column would also have to be updated accordingly.

Now what I need is to adjust this dataframe so that I have in the following manner:

        frame   requests
    0   0   214388438.0
    1   0             0
    2   0             0
    3   0             0
    4   0             0
    5   0             0
    6   0             0
   ....................
   47   0             0
   48   1   194980303.0
   49   1             0
   ....................

The thing is the dataframe is only 48 rows long but it could be longer like 200 or more so it would have to take that into account.

CodePudding user response:

Use Index.repeat with DataFrame.loc and then duplicated values set to 0:

N = 3
df = df.loc[df.index.repeat(N)]
df['requests'] = df['requests'].mask(df.index.duplicated(), 0)
df = df.reset_index(drop=True)
print (df)
    frame     requests
0       0  214388438.0
1       0          0.0
2       0          0.0
3       1  194980303.0
4       1          0.0
5       1          0.0
6       2  179475934.0
7       2          0.0
8       2          0.0
9       3  165196540.0
10      3          0.0
11      3          0.0
12      4  154815540.0
13      4          0.0
14      4          0.0
15      5  123650671.0
16      5          0.0
17      5          0.0
18      6  119089045.0
19      6          0.0
20      6          0.0
  • Related