Replicating idxmax with method chaining
A good exercise is to attempt an implementation of a built-in DataFrame method on your own. This type of replication can give you a deeper understanding of other pandas methods that you normally wouldn't have come across. .idxmax
is a challenging method to replicate using only the methods covered thus far in the book.
This recipe slowly chains together basic methods to eventually find all the row index values that contain a maximum column value.
How to do it…
- Load in the college dataset and execute the same operations as the previous recipe to get only the numeric columns that are of interest:
>>> def remove_binary_cols(df): ... binary_only = df.nunique() == 2 ... cols = binary_only[binary_only].index.tolist() ... return df.drop(columns=cols) >>> college_n = ( ... college ... .assign( ... MD_EARN_WNE_P10=pd.to_numeric( ... college.MD_EARN_WNE_P10...