concept end in category pandas

appears as: end, end
Pandas in Action

This is an excerpt from Manning's book Pandas in Action.

In  [5] movies.head(4)
 
Out [5]
 
                              Rank        Studio      Gross   Year
Title_____________________________________________________________
Avengers: Endgame                1   Buena Vista  $2,796.30   2019
Avatar                           2           Fox  $2,789.70   2009
Titanic                          3     Paramount  $2,187.50   1997
Star Wars: The Force Awakens     4   Buena Vista  $2,068.20   2015
 
…or view a slice of the end.
 
In  [6] movies.tail(6)
 
Out [6]
 
                          Rank           Studio     Gross   Year
Title___________________________________________________________
21 Jump Street             777             Sony   $201.60   2012
Yogi Bear                  778  Warner Brothers   $201.60   2010
Garfield: The Movie        779              Fox   $200.80   2004
Cats & Dogs                780  Warner Brothers   $200.70   2001
The Hunt for Red October   781        Paramount   $200.50   1990
Valkyrie                   782              MGM   $200.30   2008

The visual inconsistencies in the Name column are the first sign that something is off with the dataset. The row values are surrounded by whitespace, both at the beginning and end of the strings. On top of that, the values in the Name column are all uppercased. Here's a closer look at both issues.

What if we wanted to extract the categorical ranking ("High", "Medium", "Low", and "All") instead? This process is a bit more complex -- the lengths of the words are different. There's a few solutions available here. We can begin with the str.slice method and extract up to the beginning of each word. Passing the str.slice method a single argument will mark it as the lower bound and extract until the end of the string.

In the next example, we extract from index position 8 to the end of each string. The character at index position 8 is the first letter in each risk type (the "H" in "High", the "M" in "Medium" or the "L" in Low).

In  [28] inspections["Risk"].str.slice(8).head() # is the same as
         inspections["Risk"].str[8:].head()
 
Out [28] 0      High)
         1    Medium)
         2       Low)
         3      High)
         4      High)
         Name: Risk, dtype: object

We still have to deal with the pesky closing parentheses. One option is to provide a negative second argument to the str.slice method. Much like Python's list slicing notation, it will reflect the upper bound but relative to the end of the string. For example, -1 will extract up but not including the last character.