Yaser
Yaser

Reputation: 61

How to shift rows in python pandas dataframe

I have a dataframe in which I need to copy all 10 rows 50th times below this ten rows after dropping first row and adding new row with DFM value 100% at last in every iteration. Like below example.

df1:-

    Cal     Group       DFM
     1     period 1    39.36%
     2     period 1    98.89
     3     period 1    99.95%
     4     period 1    99.97%
     5     period 1    99.99%
     6     period 1    100.00%
     7     period 1    100.00%
     8     period 1    100.00%
     9     period 1    100.00%
    10    period 1    100.00%
     

Result:- I have printed it 3 times and shifted values 3 times. But I need do this 50 times again and again.

    Cal     Group       DFM
     1     period 1    39.36%
     2     period 1    98.89
     3     period 1    99.95%
     4     period 1    99.97%
     5     period 1    99.99%
     6     period 1    100.00%
     7     period 1    100.00%
     8     period 1    100.00%
     9     period 1    100.00%
    10    period 1    100.00%
     1      period 1    98.89
     2     period 1    99.95%
     3     period 1    99.97%
     4     period 1    99.99%
     5     period 1    100.00%
     6     period 1    100.00%
     7     period 1    100.00%
     8     period 1    100.00%
     9     period 1    100.00%
    10    period 1    100.00%
     1     period 1    99.95%
     2     period 1    99.97%
     3     period 1    99.99%
     4     period 1    100.00%
     5     period 1    100.00%
     6     period 1    100.00%
     7     period 1    100.00%
     8    period 1    100.00%
     9    period 1    100.00%
    10    period 1    100.00%

Upvotes: 1

Views: 526

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

You can use a list comprehension with shifted versions of the dataframe and then concatanate them together. To keep Cal and Group aside from computations, we set them as the index first. Also, since shifting will induce missing values in DFM, we pass the desired fill value i.e., "100.00%" to shift. Lastly, we reset the index to get Cal and Group as columns again:

df_with_new_idx = df.set_index(["Cal", "Group"])

repeat = 50
new_df = pd.concat([df_with_new_idx.shift(-j, fill_value="100.00%")
                    for j in range(repeat)])

new_df = new_df.reset_index()

For repeating 3 times instead of 50 above for illustration, I get:

>>> new_df

    Cal     Group      DFM
0     1  period 1   39.36%
1     2  period 1    98.89
2     3  period 1   99.95%
3     4  period 1   99.97%
4     5  period 1   99.99%
5     6  period 1  100.00%
6     7  period 1  100.00%
7     8  period 1  100.00%
8     9  period 1  100.00%
9    10  period 1  100.00%
10    1  period 1    98.89
11    2  period 1   99.95%
12    3  period 1   99.97%
13    4  period 1   99.99%
14    5  period 1  100.00%
15    6  period 1  100.00%
16    7  period 1  100.00%
17    8  period 1  100.00%
18    9  period 1  100.00%
19   10  period 1  100.00%
20    1  period 1   99.95%
21    2  period 1   99.97%
22    3  period 1   99.99%
23    4  period 1  100.00%
24    5  period 1  100.00%
25    6  period 1  100.00%
26    7  period 1  100.00%
27    8  period 1  100.00%
28    9  period 1  100.00%
29   10  period 1  100.00%

Upvotes: 1

Related Questions