Files

49 lines
1.3 KiB
Python
Raw Permalink Normal View History

2026-10-22 08:45:37 +02:00
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "pandas",
# "rich",
# ]
# ///
"""
Sample standalone tool: print a summary of a CSV file.
The block above is inline script metadata (PEP 723). uv reads it and runs the
script in its own throwaway environment, so the tool's dependencies stay out
of the project's environment.
Usage:
uv run tools/csv_summary.py data.csv
./tools/csv_summary.py data.csv (after chmod +x)
"""
import argparse
import pandas as pd
from rich.console import Console
from rich.table import Table
def main():
parser = argparse.ArgumentParser(description=__doc__.strip().splitlines()[0])
parser.add_argument("csv_file", help="path to the CSV file to summarise")
args = parser.parse_args()
df = pd.read_csv(args.csv_file)
table = Table(title=f"{args.csv_file} ({len(df)} rows)")
table.add_column("column")
table.add_column("dtype")
table.add_column("non-null", justify="right")
table.add_column("mean", justify="right")
for name in df.columns:
col = df[name]
mean = f"{col.mean():.4g}" if pd.api.types.is_numeric_dtype(col) else "-"
table.add_row(str(name), str(col.dtype), str(col.count()), mean)
Console().print(table)
if __name__ == "__main__":
main()