Wednesday, March 11, 2020

Happiness at work


What is happiness?
While there are a number of ways to express and define if, one particular form is challenge your brain get the work done and prove yourself that you are good at work.
Well,  I was struck at the middle of a python program for a logic.
Supposing we have a data for analysing the website visit data as given below


Repeated visit date
first Visit date
Customers
08-12-2019
09-12-2019
10-12-2019
11-12-2019
16-12-2019
17-12-2019
19-12-2019
20-12-2019
21-12-2019
08-12-2019
15
2
12
3
4
7
8
1
1
2
09-12-2019
25
4
11
2
3
6
7
0
0
1
15-12-2019
30
5
13
4
5
8
9
2
2
3
16-12-2019
42
7
8
1
1
2
3
4
7
3
17-12-2019
54
21
7
0
0
1
2
3
6
2
19-12-2019
22
6
9
2
2
3
4
5
8
4
20-12-2019
12
8
8
1
3
8
1
1
2
4

The business problem is : How many times  the customers acquired daily ,revisit in the first week of their visit ?
 We have thousands of data  for months together. Missing days cannot be added manually for sure.
Now let us look at the data, the first visit date is not continuous. The re visit date is also not continuous.
What is needed ? say on 8th Dec, we got 15 customers, and they visited ,2+12+3+4= 21 times. Like wise we need to identify for all the customers and aggregate.
If the data is in such a way that the rows start at 8th Dec and continues till 20th Dec without a break and similarly for the column, the logic is simple . It is the  sum of 7 numbers.
Here it is different. The first problem to address is to have a square matrix- Data frame.  Find the logic, the most important thing.
1.       Get the min date of first visit and the max date of the revisit
2.       Get a pd.series with date.
3.       Convert to data frame and then, left join it with the current data frame
4.       Delete the additional column that is automatically created and replace NaN with 0.
5.       The rows now will have the continuous dates, what about the columns ? Transpose and repeat the process.
6.       Transpose again to get the original matrix[ df ] and now it is complete
Wow ! we got the result. But cost me good hours to get the logic, patting on my back- the result of work- Happiness..
Now the compute the problem’s solution.
Logic:- for 1st row->  sum of 2nd column till 8th column
         For 2nd row ->  sum of 3rd column till 9th column
Now we get the catch , use 2 for loops and fix. Enjoy coding and analytical solutions..