Create a new dataframe based on a list of strings and a interval of dates
I need to create a new dataframe that is the result of generating for each of the values of dataframe 2, a time interval per hour. In such a way that dataframe 1 is only a date range and that range has to be repeated for each of the values of the other dataframe.
The value of the hours will always be sorted since it has a start date and end date range.
Input Dataframe 1:
Date | Hour |
---|---|
2021-05-15 | 1 |
2021-05-15 | 2 |
2021-05-15 | 3 |
2021-05-15 | 4 |
Input Dataframe 2:
Class |
---|
A |
B |
C |
Expected Output:
Date | Hour | Class |
---|---|---|
2021-05-15 | 1 | A |
2021-05-15 | 2 | A |
2021-05-15 | 3 | A |
2021-05-15 | 4 | A |
2021-05-15 | 1 | B |
2021-05-15 | 2 | B |
2021-05-15 | 3 | B |
2021-05-15 | 4 | B |
2021-05-15 | 1 | C |
2021-05-15 | 2 | C |
2021-05-15 | 3 | C |
2021-05-15 | 4 | C |
Thank!!
Try this:
new_df = pd.merge(df,df2,how = 'cross')