Return a Python Pandas data frame’s index to a regular column with reset_index: • 1-column index? It’s now a regular column.• Multi-index? Its columns are all regular columns. reset_index returns a new data frame. It doesn’t modify the original.
Turn a column in a Python Pandas data frame into an index with set_index: Remember, set_index returns a new data frame. It doesn’t change the original one. Turn multiple columns into a multi-index by passing a list:
Calling astype on Python Pandas series with unconvertible values? Use errors=’ignore’ to skip the error:
Python Pandas astype() returns a NEW series — it doesn’t modify in place: Watch out: You’ll get a TypeError if any value can’t be converted
How many NaNs in a Python Pandas series? Use isna (returns True/False) then sum/value_counts: Hint: Pass normalize=True for the percentage
Reading CSV into Python Pandas? Pass header to specify the row containing column names: No headers at all? Just pass headers=None: Pass a list for multi-indexed column names:
Python Pandas: pivot vs pivot_table? Both reshape data, but: – pivot: one value per cell (errors if duplicates)– pivot_table: aggregates duplicates (mean, sum, etc.) Use pivot for clean data, pivot_table for real-world messiness.
What’s the difference between join and merge in Python Pandas? • join combines based on the indexes. Perfect when both data frames refer to the same person IDs, product names, or dates• merge combines based on *any* two columns — but you must specify which ones.
Apply different aggregations to each column when grouping on a Python Pandas data frame with agg and a dict:
Apply multiple aggregations when grouping on a Python Pandas data frame with agg: Grouping on 2+ columns? Each method will be applied to each column: