0

My Current Dataset is

Month  // MonthOnBook // ClickMonth // ClickRate

2017-05           23            4    14.982306
2017-05           23           18    19.253211
2017-05           23           22    19.793899
2017-06           22           3     20.346334
2017-06           22           5     22.545454

I want the new dataset to be something like below

2017-05           23           1     0.00
2017-05           23           2     0.00
2017-05           23           3     0.00
2017-05           23           4    14.982306
2017-05           23           5    14.982306
2017-05           23           6    14.982306
....

2017-05           23           17    14.982306
2017-05           23           18    19.253211
2017-05           23           19    19.253211
...
2017-05           23           21    19.253211
2017-05           23           20    19.793899
2017-05           23           21    19.793899
....
2017-05           23           34    19.793899
2017-05           23           35    19.793899

now same thing for next month but as it has been on books for 22 months, there would be 22 rows..

How do i do that ?

Any help or guide is really appreciated

2 Answers 2

1

You can insert multi rows like below

pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))

see this page for more information pandas dataframe append

Pandas API documentation : DataFrame Append

Sign up to request clarification or add additional context in comments.

3 Comments

Please edit the answer to make clearer what documentation you are linking to. Instead of “click for more”, have a short phrase that says what is at the other end of that link. Maybe the section title in the documentation would be a good anchor text here.
@bignose and jhpratt my answer is not only link, as you see I write an answer with a line of code and added extra information for research.. I can not write the entire page here for answer..
“I can not write the entire page here for answer” — You're right, that would not be appropriate. What you should do is change “click for more” to something that summarises what you're linking to (a brief phrase, like a headline on a blog post); and also write a couple of sentences with the salient information for this answer. That way, even when the link leads nowhere some time in the future, the answer here should contain the useful information.
0

You can using reindex with groupby

s=df.groupby('Month').apply(lambda x : x.set_index('ClickMonth').reindex(list(range(1,max(x['ClickMonth'])+1))).reset_index().ffill()).reset_index(drop=True)


s.Month=s.Month.bfill();s.MonthOnBook=s.MonthOnBook.bfill();s.fillna(0,inplace=True)
s
Out[606]: 
    ClickMonth    Month  MonthOnBook  ClickRate
0            1  2017-05         23.0   0.000000
1            2  2017-05         23.0   0.000000
2            3  2017-05         23.0   0.000000
3            4  2017-05         23.0  14.982306
4            5  2017-05         23.0  14.982306
5            6  2017-05         23.0  14.982306
6            7  2017-05         23.0  14.982306
7            8  2017-05         23.0  14.982306
8            9  2017-05         23.0  14.982306
9           10  2017-05         23.0  14.982306
10          11  2017-05         23.0  14.982306
11          12  2017-05         23.0  14.982306
12          13  2017-05         23.0  14.982306
13          14  2017-05         23.0  14.982306
14          15  2017-05         23.0  14.982306
15          16  2017-05         23.0  14.982306
16          17  2017-05         23.0  14.982306
17          18  2017-05         23.0  19.253211
18          19  2017-05         23.0  19.253211
19          20  2017-05         23.0  19.253211
20          21  2017-05         23.0  19.253211
21          22  2017-05         23.0  19.793899
22           1  2017-06         22.0   0.000000
23           2  2017-06         22.0   0.000000
24           3  2017-06         22.0  20.346334
25           4  2017-06         22.0  20.346334
26           5  2017-06         22.0  22.545454

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.