2

I have data such as the following:

start_date end_date project_sales project_category project_code
2015-08-03 2015-08-06 1683 CatA 1
2015-08-02 2015-08-04 6500 CatB 2

I want to create a timeframe from date 2014-01-01 to 2020-12-01 and assign the details of the above table such as : For e.g. here I created date_period df from 2015-08-02 to 2018-08-06

date_period CatA_project_sales CatB_project_sales CatA_No_of_projects CatB_No_of_projects
2015-08-02 2166 0 1 0
2015-08-03 2166 561 1 1
2015-08-04 2166 561 1 1
2015-08-05 0 561 0 1
2015-08-06 0 561 0 1

As you can see above, project_sales are divided by the number of days it was active. I am very clear with logic but I'm struggling to come up with the code to support that.

Any help is appreciated.

Note: This code will need to run on huge dataframe dating from 2014-01-01 to 2020-12-31, requesting the help keeping this in consideration

2 Answers2

1

I think you can create a pandas.date_range() for ceating series of datetime

Function Structure

According to pandas.date_range

pandas.date_range(start=None, end=None, periods=None, freq=None, tz=None, normalize=False, name=None, closed=None, **kwargs)

So, it implement what you're aiming for, you can implement this code for creating series of datetime series, after that convert it into dataframe.

Code Syntax

date = pandas.date_range(start="2014-01-01", end="2020-12-31")
dateFrame = pd.DataFrame(date)
dateFrame.columns = ["date_period"]
dateFrame.head()

Output

| date_period |
|:-----------:|
|  2014-01-01 |
| 2014-01-02  |
| 2014-01-03  |
| 2014-01-04  |
|  2014-01-05 |
Ahmed
  • 796
  • 1
  • 5
  • 16
  • While its a part of the code but, I'm not just looking for the specific. Still Grateful for help. – UTKARSH NIPANE Jan 01 '21 at 08:24
  • If this a part of what you're looking for. That's because you've not provided more information about what you're looking of how you need the date series merge with the large dataframe you have. 2. The date keys will be merge with... Last but not least, your tried coded which is not provided. – Ahmed Jan 01 '21 at 08:54
0

I would suggest you check out this Stackoverflow question. I think that you will get the desirable answer there. Python Pandas counting and summing specific conditions

Fony Lew
  • 505
  • 4
  • 16