1

I want to repeat a specific row of pandas data frame for a given number of times.

For example, this is my data frame

df= pd.DataFrame({
'id' : ['1','1', '2', '2','2','3'],
'val' : ['2015_11','2016_2','2011_9','2011_11','2012_2','2018_2'],
'data':['a','a','b','b','b','c']
})

print(df)

input datafarme

Here, "Val" column contains date in string format. It has a specific pattern 'Year_month'. For the same "id", I want the rows repeated the number of times that is equivalent to the difference between the given "val" column values. All other columns except the val column should have the duplicated value of previous row.

The output should be:

The output should be

1

1 Answer 1

3

Using resample:

df.val = pd.to_datetime(df.val, format='%Y_%m')
out = df.set_index('val').groupby('id').data.resample('1m').ffill().reset_index()
out.assign(val=out.val.dt.strftime('%Y_%m'))

   id      val data
0   1  2015_11    a
1   1  2015_12    a
2   1  2016_01    a
3   1  2016_02    a
4   2  2011_09    b
5   2  2011_10    b
6   2  2011_11    b
7   2  2011_12    b
8   2  2012_01    b
9   2  2012_02    b
10  3  2018_02    c
Sign up to request clarification or add additional context in comments.

Comments

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.