I have one pandas Dataframe like below:
import pandas as pd
df = pd.DataFrame({'name': ['AAA','BBB','CCC'],
'2017-01-06': ['3','3','4'],
'2017-01-13': ['2','1','5'],
'2017-01-20': ['1','3','4'],
'2017-01-27': ['8','3','5'],
'average_count': ['4','3','5']})
df = df.reindex_axis(['name','2017-01-06','2017-01-13','2017-01-20','2017-01-27','average_count'], axis=1)
print df
name 2017-01-06 2017-01-13 2017-01-20 2017-01-27 average_count
0 AAA 3 2 1 8 4
1 BBB 3 1 3 3 3
2 CCC 4 5 4 5 5
I want to one output dataframe with four columns : name,date,count,average_count.
- name column contains name from the above dataframe.
- date column contains four different dates per single name.
- count column contains count values for respective date.
- average_count contains four different average count values.
If the months first week it is then average count need to calculate with (count of first week) / 1.
For 2nd week, (count of first week+count of first week) / 2.
For 3rd week, (count of first week+count of second week+count of third week) / 3.
For 4th week, (count of first week+count of second week+count of third week+count of fourth week) / 4.
In one month maximum five weeks are available (Need to handle five week scenario as well).
Edit1: Average count value calculation
This average count value is truncated like if the value <= 2.49 i.e. 2 and value >= 2.50 i.e. 3.
Output Dataframe looks like below:
name date count average_count
0 AAA 2017-01-06 3 3
1 AAA 2017-01-13 2 2
3 AAA 2017-01-20 1 2
3 AAA 2017-01-27 8 4
4 BBB 2017-01-06 3 3
5 BBB 2017-01-13 1 2
6 BBB 2017-01-20 3 3
7 BBB 2017-01-27 3 3
8 CCC 2017-01-06 4 4
9 CCC 2017-01-13 5 5
10 CCC 2017-01-20 4 3
11 CCC 2017-01-27 5 5