2

I have the following dataframe:

    n   startdate     enddate  count
0   1  2014-02-01  2014-02-01   6069
1   1  2014-02-01  2014-03-01   1837
2   1  2014-02-01  2014-04-01    107
3   1  2014-02-01  2014-05-01     54
4   1  2014-03-01  2014-03-01  10742
5   1  2014-03-01  2014-04-01   2709
6   1  2014-03-01  2014-05-01   1387
7   1  2014-04-01  2014-04-01   5584
8   1  2014-04-01  2014-05-01   1103
9   1  2014-05-01  2014-05-01   2970

Is it possible to transform it into something similar to:

    n  startdate   2014-02-01  2014-03-01 2014-04-01   2014-05-01
0   1  2014-02-01  6069        1837       107          54
1   1  2014-03-01  0           10742      2709         1387
2   1  2014-04-01  0           0          5584         1103
3   1  2014-05-01  0           0          0            2970

Thanks!

1
  • add how you created the dataframe. but some hints: play around with the functions groupby, unstack, and maybe sum Commented Jun 2, 2014 at 13:36

1 Answer 1

5

without recreating your dataframe, i think it's going to be something like:

df.groupby(['n', 'startdate', 'enddate']).sum().unstack()

you might need to fill some nan values as well:

df.fillna(0, inplace=True)
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.