Home > Software engineering >  Python multiply string column with daterange
Python multiply string column with daterange

Time:06-02

I have a dataframe which appears as following:

list_name
---------
list1
list2
...

I try to the multiply each row with a date range :

start = datetime.date(2019, 1, 1)
end = datetime.date(2025, 1, 1)
daterange = pd.date_range(start, end)

Desired result is the following:

list_name|   date
---------|  --------
list1    |  2019-01-01
list1    |  2019-01-02
list1    |  2019-01-03
  ...    |       ...
list1    |  2025-01-01
list2    |  2019-01-01
list2    |  2019-01-02
list2    |  2019-01-03
  ...    |       ...
list2    |  2025-01-01

I am not sure how to explode it across the rows.

CodePudding user response:

You can use a pd.merge(how = 'cross') to achieve this

df = pd.DataFrame({
    'list_name' : ['list1', 'list2']
})
start = datetime.date(2019, 1, 1)
end = datetime.date(2025, 1, 1)
daterange = pd.DataFrame(pd.date_range(start, end))
pd.merge(df, daterange, how = 'cross')df = pd.DataFrame({
    'list_name' : ['list1', 'list2']
})
start = datetime.date(2019, 1, 1)
end = datetime.date(2025, 1, 1)
daterange = pd.DataFrame(pd.date_range(start, end))
pd.merge(df, daterange, how = 'cross')

CodePudding user response:

The solution, thanks to @QuangHoang's comment:

from itertools import product

pd.DataFrame(list(product(df.list_name, daterange)), columns=['list_name', 'date'])
  • Related