1
00:00:00,520 --> 00:00:04,330
What day of the week are people the happiest?

2
00:00:04,780 --> 00:00:10,780
Can we answer that question by using Python and the data we have at hand right now?

3
00:00:11,320 --> 00:00:13,030
Well, I think, yes, we can do that.

4
00:00:13,240 --> 00:00:15,810
And that is the beauty of data analysis.

5
00:00:16,100 --> 00:00:20,900
If you are a bit creative, you can answer very interesting questions about your data.

6
00:00:21,430 --> 00:00:27,640
So in this video, we are going to find out together which day of the week are people the happiest?

7
00:00:28,000 --> 00:00:29,420
How do we do that?

8
00:00:29,440 --> 00:00:31,890
Well, by generating a graph.

9
00:00:32,320 --> 00:00:34,010
So how can we do that?

10
00:00:34,330 --> 00:00:41,500
Well, in my opinion, we can use the logic that if the average rating for all the courses, let's say,

11
00:00:41,500 --> 00:00:48,250
on Wednesdays is of the highest of the week, then we can see that people maybe in that day

12
00:00:48,560 --> 00:00:51,610
so on Wednesdays are more positive.

13
00:00:51,790 --> 00:00:55,690
And I'm sure we can tell that because we have so much data here.

14
00:00:55,840 --> 00:00:58,090
So thousands of ratings.

15
00:00:58,090 --> 00:01:03,610
And statistically, that I think gives us a lot of confidence in the outcome.

16
00:01:04,000 --> 00:01:11,110
So what we need to do is we need to create a graph which is going to have seven days in the horizontal

17
00:01:11,110 --> 00:01:11,540
axis.

18
00:01:11,740 --> 00:01:19,510
So from Monday to Sunday and in the vertical axis, we are going to have the average for each day.

19
00:01:20,470 --> 00:01:24,160
That means we have to do a lot of data aggregation.

20
00:01:25,000 --> 00:01:25,870
So let's do that.

21
00:01:29,770 --> 00:01:34,210
What day are people that are happiest?

22
00:01:34,510 --> 00:01:43,690
Now, we are going to use, as always, the data data frame on so where we have these ratings left at a particular

23
00:01:43,690 --> 00:01:44,310
time stamps.

24
00:01:44,920 --> 00:01:50,530
So what do we need to obstruct where the weekday, let's call it like that.

25
00:01:52,690 --> 00:02:00,250
Which is equal to data, timestamp, so out of timestamp, we access the dt property and out of that

26
00:02:00,250 --> 00:02:06,000
we need strftime and the day of the week.

27
00:02:06,070 --> 00:02:13,510
So, like, if you want Sunday or Monday or Tuesday, that name of the day of the week can be extracted

28
00:02:13,510 --> 00:02:18,010
using the capita A format code.

29
00:02:18,370 --> 00:02:19,730
You can look that up on Google.

30
00:02:19,750 --> 00:02:20,720
I do that as well.

31
00:02:20,740 --> 00:02:22,990
It's impossible to to remember everything.

32
00:02:23,540 --> 00:02:29,980
Now, we can print out the data again and we see that we have a weekday column.

33
00:02:30,430 --> 00:02:33,190
So Friday, Friday, Friday is repeating over and over again.

34
00:02:33,400 --> 00:02:37,740
And we need to aggregate now to get the average for each weekday.

35
00:02:38,470 --> 00:02:47,970
So let's say a weekday_average for the data frame that is going to contain the aggregated data.

36
00:02:48,370 --> 00:02:51,420
Again, groupby is our savior.

37
00:02:51,930 --> 00:02:52,290
Hmm.

38
00:02:52,900 --> 00:02:54,480
What do we use this time?

39
00:02:55,120 --> 00:02:56,140
Well, weekday.

40
00:02:57,860 --> 00:03:09,980
And then do mean and let's see weekday average what we get and that's it, then all we have to do is

41
00:03:10,640 --> 00:03:11,090
leave that.

42
00:03:11,720 --> 00:03:21,770
All we have to do is plt.plot the x axis would be weekday_average

43
00:03:22,190 --> 00:03:29,630
dot index and the Y axis would be weakday_average, weekday, sorry rating.

44
00:03:31,760 --> 00:03:38,720
Plot and this is the answer, I know the order is not as you would expect, but we can fix that.

45
00:03:39,840 --> 00:03:48,300
Either way, we can tell that Friday is quite different from the other days, so on Fridays, people

46
00:03:48,300 --> 00:03:58,260
leave a four point four, five five average rating on a scale of one to five, and that makes sense.

47
00:03:58,560 --> 00:04:02,970
I think I also fall in that range, so I tend to be happier on Fridays.

48
00:04:03,780 --> 00:04:07,740
So end of the week and the weekend is coming, so it makes sense.

49
00:04:08,310 --> 00:04:14,710
Now, how can we order these data, so we need to order the weekday column.

50
00:04:15,750 --> 00:04:17,130
Well, we can do that.

51
00:04:17,520 --> 00:04:20,760
Let me comment this out for a while.

52
00:04:21,510 --> 00:04:29,940
Weekday_average equal to weekday_average.sort_values.

53
00:04:31,410 --> 00:04:44,940
By weekday and then print out and see what weekday_average is, and you're going to see that this is not

54
00:04:45,180 --> 00:04:52,470
the order we would expect. That is happening, of course, because the index column, this one here,

55
00:04:53,850 --> 00:04:55,470
has string values.

56
00:04:55,500 --> 00:04:58,800
So Friday, Mondays are they are all strings.

57
00:04:59,460 --> 00:05:05,050
Therefore, Python is ordering them in alphabetical order.

58
00:05:05,220 --> 00:05:07,230
So Friday, first Monday and so on.

59
00:05:07,770 --> 00:05:12,060
They are strings because this method produces a string

60
00:05:12,060 --> 00:05:16,800
weekday column, you see, string from time.

61
00:05:17,460 --> 00:05:21,150
So timestamp was time, but we converted to a string.

62
00:05:21,660 --> 00:05:25,080
Now there may be different workarounds to fix this.

63
00:05:25,560 --> 00:05:31,110
The one that comes to my mind is that we could add a weekday

64
00:05:32,490 --> 00:05:33,840
number.

65
00:05:35,520 --> 00:05:38,280
Or simply daynumber.

66
00:05:43,220 --> 00:05:51,200
To the data data frame and all of that again uses string from time, but this time we are going to

67
00:05:51,200 --> 00:05:59,190
use percentage lowercase w and what that would give us is.

68
00:05:59,240 --> 00:06:07,040
Let me show you the data now is this data frame and it says daynumber.

69
00:06:07,280 --> 00:06:16,430
And so now we have a number attached to each row besides the name of the weekday that allows us now

70
00:06:16,430 --> 00:06:20,420
to sort the values by this day number column.

71
00:06:21,020 --> 00:06:22,550
So Monday is the first one.

72
00:06:23,910 --> 00:06:25,680
Friday is the fifth and so on.

73
00:06:27,750 --> 00:06:28,410
Let's do that.

74
00:06:28,950 --> 00:06:36,600
I'm going to delete that data data frame from there, the printing uncomment this and change that to

75
00:06:36,750 --> 00:06:38,250
daynumber.

76
00:06:41,200 --> 00:06:42,580
And here weekday

77
00:06:42,790 --> 00:06:50,200
average, delete, uncomment this, execute, we have this Key Error day

78
00:06:50,210 --> 00:06:50,830
number.

79
00:06:51,810 --> 00:06:55,320
Oh, because I forgot to add daynumber in here.

80
00:06:59,050 --> 00:07:00,100
Again, we have an error.

81
00:07:00,130 --> 00:07:02,400
Let's see where the error occurred.

82
00:07:06,190 --> 00:07:16,180
So these are the errors deep in the libraries, you see in matplotlib and these shows the error in our

83
00:07:16,840 --> 00:07:17,340
code.

84
00:07:18,070 --> 00:07:20,560
So it's in this line, plt.plot.

85
00:07:24,480 --> 00:07:29,250
And it says that value must be an instance of string or bytes, not a tuple.

86
00:07:30,450 --> 00:07:33,380
So let's try to troubleshoot the error here.

87
00:07:33,640 --> 00:07:39,510
I'm going to uncomment this and see what's weekday average is.

88
00:07:41,910 --> 00:07:51,630
So the problem here is that weekday_average.index returns a double index, which looks like

89
00:07:52,170 --> 00:07:52,770
this.

90
00:07:55,410 --> 00:07:59,330
So it's a multi index, it contains weekday and daynumber.

91
00:08:00,030 --> 00:08:10,470
Therefore, the plot method will be confused so it doesn't know which of these arrays has to plot along

92
00:08:10,470 --> 00:08:11,380
the x axis.

93
00:08:12,690 --> 00:08:17,130
So we need a way to extract only these columns.

94
00:08:17,130 --> 00:08:26,370
So the names of the weekdays, you can do that by using the get level values.

95
00:08:26,820 --> 00:08:29,220
That is a method that expects an argument.

96
00:08:29,430 --> 00:08:35,910
In this case, we can either input zero or one. Zero means we are extracting, we are accessing the first

97
00:08:35,910 --> 00:08:40,280
column of the multi index and one would be the second column.

98
00:08:40,530 --> 00:08:41,850
So we need the first column.

99
00:08:43,790 --> 00:08:54,800
And that gives us those days, so the names of the days.
Therefore now we can make use of that in here

100
00:08:54,800 --> 00:08:58,580
so we can copy that expression.

101
00:08:58,730 --> 00:09:03,050
That is the array we want to plot along the X axis.

102
00:09:03,300 --> 00:09:06,310
So we put that in here, execute.

103
00:09:06,350 --> 00:09:08,000
And this is the output.

104
00:09:08,930 --> 00:09:20,140
If we want to make the picture wider, we need to do plt.figure 
figsize equal to a list of 25 ,3.

105
00:09:20,150 --> 00:09:22,970
It works well or maybe a bit.

106
00:09:25,650 --> 00:09:26,010
Smaller.

107
00:09:27,030 --> 00:09:28,060
Yeah, this is better.

108
00:09:28,260 --> 00:09:32,670
So you have to to find a good ratio between the width and the height.

109
00:09:33,120 --> 00:09:39,500
And again, we see that Friday is the day that has the highest average rating.

110
00:09:39,840 --> 00:09:41,670
And so that was about this video.

111
00:09:42,150 --> 00:09:43,170
Thanks a lot for following!

112
00:09:43,170 --> 00:09:45,420
I'll talk to you in the next videos! See you!

