1

I have a pandas dataframe with 3 columns DateIndex, Country, and Awareness

I need to convert the DateIndex to individual date entries per row.

Input:

Country Awareness DateIndex

USA 50% DatetimeIndex(['2017-05-01', '2017-05-02', '20... UK 75% DatetimeIndex(['2018-05-01', '2018-05-02', '20... Output: Country Awareness Date USA 50% 2017-05-01 USA 50% 2017-05-02 . . UK 75% 2018-05-01
UK 75% 2018-05-02

user2419259
  • 93
  • 1
  • 8

1 Answers1

1

Solution for pandas 0.25+ with DataFrame.explode:

df = pd.DataFrame({'Country':['USA','UK'],
                   'Awareness':['50%','75%'],
                   'Datetime':[pd.date_range('2017-05-01','2017-05-03'),
                               pd.date_range('2017-05-01','2017-05-04')]})
df = df.explode('Datetime')
print (df)
  Country Awareness   Datetime
0     USA       50% 2017-05-01
0     USA       50% 2017-05-02
0     USA       50% 2017-05-03
1      UK       75% 2017-05-01
1      UK       75% 2017-05-02
1      UK       75% 2017-05-03
1      UK       75% 2017-05-04
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252