AI Skill Report Card
Summing CSV Rows
Quick Start14 / 15
Pythonimport pandas as pd def add_totals_row(input_path, output_path=None, label_col=None): df = pd.read_csv(input_path) output_path = output_path or input_path totals = {} for col in df.columns: if pd.api.types.is_numeric_dtype(df[col]): totals[col] = df[col].sum() else: totals[col] = "" # Put a "Total" label in the first non-numeric column (or specified label_col) label_target = label_col or next( (c for c in df.columns if not pd.api.types.is_numeric_dtype(df[c])), df.columns[0] ) totals[label_target] = "Total" df_out = pd.concat([df, pd.DataFrame([totals])], ignore_index=True) df_out.to_csv(output_path, index=False) return df_out add_totals_row("data.csv", "data_with_totals.csv")
For Excel files with multiple sheets:
Pythondef add_totals_to_all_sheets(input_path, output_path): sheets = pd.read_excel(input_path, sheet_name=None) with pd.ExcelWriter(output_path, engine="openpyxl") as writer: for name, df in sheets.items(): totals = { col: df[col].sum() if pd.api.types.is_numeric_dtype(df[col]) else "" for col in df.columns } label_col = next( (c for c in df.columns if not pd.api.types.is_numeric_dtype(df[c])), df.columns[0] ) totals[label_col] = "Total" df_out = pd.concat([df, pd.DataFrame([totals])], ignore_index=True) df_out.to_excel(writer, sheet_name=name, index=False)
Recommendation▾
Add handling for the 'running totals function twice' pitfall directly in the code (check for existing Total row) rather than just mentioning it in prose
Workflow13 / 15
Progress:
- Identify input format (single CSV, multi-sheet Excel, or folder of CSVs)
- Load each sheet/file into a DataFrame
- Detect numeric columns automatically (don't hardcode column names)
- Compute column sums, skip/blank non-numeric columns
- Insert a clear label ("Total", "Sum", or "TOTAL") in the leftmost text column
- Append totals as the final row, preserving original column order
- Write output (overwrite or new file, per user preference)
- Verify row/column counts: output rows = input rows + 1
Recommendation▾
Include an example showing a mixed-type column edge case or an ID-like numeric column being flagged/excluded
Examples16 / 20
Example 1:
Input (sales.csv):
Region,Q1,Q2,Q3
East,100,150,200
West,80,90,110
Output (sales.csv):
Region,Q1,Q2,Q3
East,100,150,200
West,80,90,110
Total,180,240,310
Example 2:
Input: Excel workbook with sheets "Jan" and "Feb", each with a Date, Amount, Units columns.
Output: Same workbook, each sheet gets a final row Total,,<sum_amount>,<sum_units> (Date left blank since it's non-numeric but not the label column).
Example 3: Input: Folder of 5 CSVs with identical schema. Output: Each CSV processed independently, each gets its own totals row — no cross-file aggregation unless explicitly requested.
Recommendation▾
Consider adding a small CLI or function signature for folder-of-CSVs batch processing mentioned in Example 3, since only single-file and multi-sheet code is shown
Best Practices
- Never hardcode column names — detect numeric dtypes dynamically so the skill works on any schema.
- Preserve original row order and column order exactly; only append, never insert mid-table.
- Use a clearly recognizable label ("Total") rather than leaving it blank, so the totals row is unambiguous later.
- When processing multiple sheets/files, treat each independently unless the user explicitly asks for a grand total across all of them.
- Keep a backup or write to a new file by default unless the user confirms in-place overwrite is fine.
- Handle missing/NaN values with
sum(skipna=True)(pandas default) rather than erroring out. - If a column is mixed type (e.g., numeric with some text), flag it instead of silently guessing.
Common Pitfalls
- Summing columns that are numeric-looking IDs (e.g., "Year", "ZIP Code") — confirm with the user or exclude obvious ID-like columns.
- Forgetting that the totals row itself could get summed again if the function is run twice on the same file — check for an existing "Total" row before appending.
- Mixing up sheet-level totals vs. a single grand total across all sheets — clarify which is wanted.
- Writing totals as strings instead of numeric types, breaking downstream formulas in Excel.
- Losing original formatting (currency symbols, decimal places) when round-tripping through pandas — reapply formatting on output if needed.