For our second exercise what I'd like you to do is find the email with the most number of words. 1
So this is after cleaning, 2
right. 3
In this challenge, I'd like you to print out the number of words in the longest email - that is after cleaning 4
and stemming and I'd like you to note the longest email's position in the list of cleaned emails. 5
Also print out the stemmed list of words in the longest e-mail and print out the longest email from 6
the data dataframe. I'll give you a few seconds to pause the video and give this a go. 7
If you want a hint, use the length function, "len" and practice the Python list comprehension. 8
All right. 9
So here's the solution. 10
One way to do this is to use a for loop. 11
Yeah. 12
Classic. 13
In this case, we would create an empty list say "clean_email_lengths" which is going to hold 14
on to the number of characters in each email. 15
So an empty list is created with an empty pair of square brackets. 16
And then we write our for loop, so "for sublist in stemmed_nested_list", 17
this is where we've stored all our email bodies, right. 18
This is what we can iterate over and check the number of characters and we can check the number of characters 19
with the "len" function so "len(sublist)" will check for the number of characters. 20
But what we actually want to do with these number of characters is we want to append them to our empty 21
list up here as the loop runs. 22
So it'd be "clean_email_lengths.append(len(sublist))" and then 23
two closing parentheses. Let's take a look at what this looks like. 24
So I'm going to hit Shift+Enter and then maybe print my clean_email_lengths. What I can see here is that the first 25
email has 50 characters after stemming and after removing stop once, that is. The next one has 80 characters, 26
the next one has 92. 27
So this seems to work. 28
But one thing we can do is, instead of using this for loop, we can also do it a very Python way, 29
we can use Python 30
list comprehension, 31
right? If we want to do it this way we can take our clean_email_lengths variable and simply set that 32
equal to the result of the Python list comprehension. 33
The bit that we want to append of course is "len(sublist)" and the for loop would go inside 34
these parentheses. 35
So "for sublist in stemmed_nested_list". This is how we can do it in the Python list comprehension way. 36
To print out the number of words in the longest email, 37
how would we do it? 38
Well, there's a Python function for finding the largest value in a list and that's the "max" 39
function - "max(clean_email_lenghts)" will give us the largest value in this list. 40
In this case, the largest value is 7661. 41
This is, these are the number of characters in the longest email. In terms of where this email is, in terms 42
of its position, you would have had to do a little bit of googling right. 43
You would have had to find the position of this value, 44
7661 in the clean email lengths list. 45
So the email position in the list and also the data dataframe, because they match, right, is going to 46
be found at "np.argmax( 47
clean_email_lenghts)", so numpy has a handy, handy function called "argmax" 48
which will give us the location of the largest value in this list. 49
So figuring this out was the second part of the challenge if you will. Now, 50
now this isn't the only way. 51
And if you have another favorite way that you solve this problem please share it in the comments below 52
this lesson and I'd be curious to have read and find out how you solved this problem. So let me hit Shift+ 53
Enter to find out where this email is. 54
It's at position 5401. Bringing up the list of words in this email should 55
be fairly simple, 56
because all I have to do is feed this value into the square brackets for my stemmed_nested_list, so "np. 57
argmax( 58
clean_email_lengths)" will show me what the words are that are in this list, so 59
this is 5600 odd words long. Now, 60
what about pulling out the original email from the dataframe? 61
In this case, I would use "data.at" because I know exactly which document ID we're going to supply, 62
namely "np.argmax(clean_email_lengths)". 63
So this is for the row name, right. Row location would be "iat", 64
but row name would be "at", but we've handily named our rows after integers so we can do it this way, 65
but for the column, because we don't want the entire row, 66
we'll just supply the name of the column after the comma, so it'll be a 'MESSAGE' and this is it. 67
This is the original email 68
after removing the header. So you can see it's quite long. 69
Brilliant. 70
So I hope you solved this challenge on your own 71
and the solution was helpful in comparing your code with with mine. In the next lessons, 72
we're going to go back to working a bit more with our dataframes. 73
I'll see you there.