So I have pandas dataframe df_dates as below.
PERSON_ID MIN_DATE MAX_DATE
0 000099-48 2016-02-01 2017-03-20
1 000184 2016-02-05 2017-01-19
2 000461-48 2016-03-07 2017-03-20
3 000791-48 2016-02-01 2017-03-07
4 000986-48 2016-02-01 2017-03-17
5 001617 2016-02-01 2017-02-20
6 001768-48 2016-02-01 2017-03-20
7 001937 2016-02-01 2017-03-17
8 002223-48 2016-02-04 2017-03-16
9 002481-48 2016-02-05 2017-03-17
I am trying to add all dates between the Min and Max as row each Person_ID. Here is what tried.
df_dates.groupby('PERSON_ID').apply(lambda x: pd.date_range(x['MIN_DATE'].values[0], x['MAX_DATE'].values[0]))
But what I get with this is, is there any way to transpose that series into rows for each Person_ID? or any other better way of doing it?
PERSON_ID
0-L2ID DatetimeIndex(['2016-08-05', '2016-08-06', '20...
0-LlID DatetimeIndex(['2016-02-03', '2016-02-04', '20...
000099-48 DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000184 DatetimeIndex(['2016-02-05', '2016-02-06', '20...
000276 DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000461-48 DatetimeIndex(['2016-03-07', '2016-03-08', '20...
000493-48 DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000615-48 DatetimeIndex(['2016-02-02', '2016-02-03', '20...
000791-48 DatetimeIndex(['2016-02-01', '2016-02-02', '20...
000986-48 DatetimeIndex(['2016-02-01', '2016-02-02', '20...
dtype: object
Here is what I am trying achieve:
PERSON_ID Date
000099-48 2/1/2016
000099-48 2/2/2016
000099-48 2/3/2016
000099-48 2/4/2016
:
:
000099-48 3/18/2016
000099-48 3/19/2016
000099-48 3/20/2016
000184 2/5/2016
000184 2/6/2016
000184 2/7/2016
:
:
000184 1/17/2017
000184 1/18/2017
000184 1/19/2017
You can reshape using
melt
, then perform agroupby
andresample
:The resulting output:
Option 1
Option 2
You can also continue what you were already doing before, but convert the datetimeindex into a string, and then use
str.split
to create new rowsFor example:
Create Function to convert to string.
Split the string into new rows.
Join with PERSON_ID column.