If your Python Pandas data frame has a datetime column, you can group on a portion of that via the “dt” accessor. For example, let’s get mean trip_distance for each hour of the day:
Does Python Pandas allow multiple data columns and multiple methods? Yes! The above returns a data frame. Its 4 columns are a 2-level multi-index with mean/std in the inner level.
Does Python Pandas allow multiple categorical columns and multiple methods? Yes! The above returns: – A 2-column data frame (mean + std)– A 2-level index (passenger_count + VendorID)
groupby on a single column in Python Pandas returns a series. Want a single-column data frame? Instead of pass a one-element list of data columns:
Invoke “groupby” with two categorical columns in Python Pandas, and get a two-part multi-index: Turn into a data frame with unstack:
A “groupby” call in Python Pandas is normally sorted by index. But if you’re grouping by month name, April will come before January. Pass “sort=False”, and the index will reflect the order of appearance:
The simplest grouping in Python Pandas is groupby: For example: Returns a series whose index is the unique values from passenger_count.
You can remove NaN from a Python Pandas data frame with dropna, but be careful: It removes rows with even one NaN, which can be overkill. Pass “thresh” to allow some rows to remain, even if they contain NaN:
Coming to Python Pandas from NumPy? You’ll reach for np.isnan: Unfortunately, this works. Better, use s.isna (or s.isnull). But the best way to drop NaN? Use dropna:
I’m a big fan of exercises, which is why the LernerPython platform includes hundreds of them — all using my in-browser practice system, which handles Python, Pandas, and Git, along with an AI-based Socratic tutor based on my writing. As of today, the practice system is even better: It includes a visual debugger, allowing you…