Replicating idxmax with method chaining
It can be a good exercise 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.
Getting ready
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:
>>> college = pd.read_csv('data/college.csv', index_col='INSTNM') >>> cols = ['MD_EARN_WNE_P10', 'GRAD_DEBT_MDN_SUPP'] >>> for col in cols: college[col] = pd.to_numeric(college[col], errors='coerce') >>> college_n = college.select_dtypes(include=[np.number]) >>> criteria = college_n.nunique...