Skip to content

Instantly share code, notes, and snippets.

@jtrive84
Created April 6, 2026 13:20
Show Gist options
  • Select an option

  • Save jtrive84/748d6c070f350faab7699680cb7dfe46 to your computer and use it in GitHub Desktop.

Select an option

Save jtrive84/748d6c070f350faab7699680cb7dfe46 to your computer and use it in GitHub Desktop.
Glimpse DataFrame
class GlimpseDataFrame(pd.DataFrame):
"""DataFrame subclass with a glimpse method similar to Polars."""
def glimpse(self, max_cols=10):
"""
Display a transposed view of the first few rows, showing column names and types.
Parameters:
-----------
max_cols : int
Maximum number of columns to display
"""
n_cols = min(len(self.columns), max_cols)
cols_to_show = self.columns[:n_cols]
print(f"shape: ({len(self)}, {len(self.columns)})")
print()
for col in cols_to_show:
dtype = str(self[col].dtype)
first_val = self[col].iloc[0] if len(self) > 0 else None
print(f"{col}: {dtype}")
print(f" {first_val}")
if len(self.columns) > max_cols:
print(f"... and {len(self.columns) - max_cols} more columns")
@property
def _constructor(self):
return GlimpseDataFrame
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment