1

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%
0

1 Answer 1

1

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%
Sign up to request clarification or add additional context in comments.

2 Comments

I need your help on this stackoverflow.com/q/68241266/16292010
looks like you have an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.