Standardize formatting with black and apply ruff auto-fixes. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
70 lines
1.6 KiB
Python
70 lines
1.6 KiB
Python
"""
|
|
Script to rename additional defense CSV column headers.
|
|
"""
|
|
|
|
import pandas as pd
|
|
import os
|
|
|
|
# Column mapping
|
|
COLUMN_MAPPING = {
|
|
"Fld%": "fielding_perc",
|
|
"Rtot": "tz_runs_total",
|
|
"Rtot/yr": "tz_runs_total_per_season",
|
|
"Rtz": "tz_runs_field",
|
|
"Rdp": "tz_runs_infield",
|
|
}
|
|
|
|
# Directory with defense files
|
|
DATA_DIR = "data-input/2005 Live Cardset/"
|
|
|
|
# All defense files
|
|
DEFENSE_FILES = [
|
|
"defense_c.csv",
|
|
"defense_1b.csv",
|
|
"defense_2b.csv",
|
|
"defense_3b.csv",
|
|
"defense_ss.csv",
|
|
"defense_lf.csv",
|
|
"defense_cf.csv",
|
|
"defense_rf.csv",
|
|
"defense_of.csv",
|
|
"defense_p.csv",
|
|
]
|
|
|
|
print("Renaming additional defense CSV columns...")
|
|
print("Column mappings:")
|
|
for old, new in COLUMN_MAPPING.items():
|
|
print(f" {old} -> {new}")
|
|
print()
|
|
|
|
for filename in DEFENSE_FILES:
|
|
filepath = os.path.join(DATA_DIR, filename)
|
|
|
|
if not os.path.exists(filepath):
|
|
print(f"⚠ Skipping {filename} (not found)")
|
|
continue
|
|
|
|
# Read CSV
|
|
df = pd.read_csv(filepath)
|
|
|
|
# Track which columns were renamed
|
|
renamed = []
|
|
for old_col, new_col in COLUMN_MAPPING.items():
|
|
if old_col in df.columns:
|
|
renamed.append(f"{old_col} -> {new_col}")
|
|
|
|
# Rename columns
|
|
df = df.rename(columns=COLUMN_MAPPING)
|
|
|
|
# Save back
|
|
df.to_csv(filepath, index=False)
|
|
|
|
if renamed:
|
|
print(f"✓ {filename}: Renamed {len(renamed)} columns")
|
|
for r in renamed:
|
|
print(f" {r}")
|
|
else:
|
|
print(f" {filename}: No matching columns found")
|
|
|
|
print("\n✓ All defense files processed successfully!")
|