⚙️ File Operations · 6 min read
How to Add Row Numbers to a CSV File (Excel, Sheets, Python)
Add a column, number it 1 to n, and save. Excel: type 1 and 2, select both, drag the fill handle down. Google Sheets: =SEQUENCE(COUNTA(A2:A)). Python: write each row with enumerate(reader, start=1). The numbering is the easy part; the trap is that the number you add will not match the line numbers in the file if any field contains a line break.
That last point decides the approach. If the CSV came out of a spreadsheet it probably has no embedded line breaks, and any method works. If it came from a database export, a CRM or a text field with addresses and notes, quoted multi-line fields are common and half of the naive recipes will number the wrong rows.
Excel
Insert a column at the far left, type 1 in the first data cell and 2 in the next, select both cells and drag the small square at the bottom-right corner down to the last row. Excel fills 1, 2, 3 and so on. For a long file, double-click the fill handle instead of dragging: Excel fills to the end of the adjacent data automatically, up to the sheet limit of 1,048,576 rows.
Two alternatives for the same result: =ROW()-1 in each row (adjust the offset if your header is on row 1), or Home then Fill then Series with a step of 1.
Then save with File then Save As and choose CSV UTF-8, not plain CSV. Plain CSV writes the Windows ANSI code page, which will mangle any accented name in the file; CSV UTF-8 writes UTF-8 with a byte-order mark, which Excel reads back correctly.
Google Sheets
In the first empty column next to the data, =ROW()-1 and fill down, or use one formula that spills: =SEQUENCE(COUNTA(A2:A)). The COUNTA counts non-empty cells in the data column, so the sequence stops at the real last row instead of running to the bottom of the sheet.
Download with File then Download then Comma-separated values. Sheets writes UTF-8 without a byte-order mark, which is the friendlier choice for scripts and worse for a colleague who will double-click the file in Excel.
Python
The csv module handles quoting, embedded commas and embedded line breaks correctly, which is exactly why it is worth three lines of code:
import csv
with open("in.csv", newline="", encoding="utf-8") as f, \
open("out.csv", "w", newline="", encoding="utf-8") as g:
reader = csv.reader(f)
writer = csv.writer(g)
header = next(reader)
writer.writerow(["row_no"] + header)
for n, row in enumerate(reader, start=1):
writer.writerow([n] + row)Both files are opened with newline="", which the Python documentation requires for the csv module; without it, a quoted field containing a line break is written with the wrong line ending and some parsers split the record. Use encoding="utf-8-sig" on the output if the file is destined for Excel.
For a pandas workflow the same column is one line: df.insert(0, "row_no", range(1, len(df) + 1)), then df.to_csv("out.csv", index=False).
Command Line, With a Caveat
nl -s, -w1 in.csv numbers every physical line. So does awk. Both are fine for a file with one line per record and both are wrong for a file with quoted line breaks, because they count newlines rather than records. The symptom is a file where some numbers skip: a three-line address inside one quoted field eats three numbers.
If you need a shell one-liner anyway, check first. A record count from a real CSV parser that differs from wc -l is the signal that this file needs the Python route.
The Two Traps
| Trap | What happens |
|---|---|
| Quoted fields with line breaks | Line-based tools number the wrong rows and the file has fewer records than lines |
| Sorting after numbering | Numbers no longer describe order and any join on them silently mismatches |
| Extra column downstream | Loaders with a fixed schema reject the file or shift every field by one |
On the third trap: adding a column changes the field count for everything that reads the file. If a scheduled import expects 12 columns and now sees 13, some tools fail loudly and others put your counter into the first data field. Add the number column at the end if you can, and tell whoever owns the import.
When a Row Number Is the Wrong Tool
A row number answers the question "where is this line right now". An ID answers "which record is this". The moment the file will be sorted, merged or re-exported, you want the second. Generate a stable value from the record itself, or keep an existing key column and leave numbering to the viewer, which already shows line numbers.
If the goal is only to discuss specific rows with someone, the spreadsheet's own row numbers do the job with no file change at all. Adding a counter column is worth it when the file will be split, compared with a second export, or reviewed as plain text.
Verify the Result
Three checks take ten seconds. The last number should equal the number of data rows, not one more. The header should not carry a number. And the first data row should still line up with its original values, which catches an off-by-one shift that numbering alone would hide.
Frequently Asked Questions
How do I add a row number column to a CSV?
In Excel, 1 and 2 then drag the fill handle. In Google Sheets, =SEQUENCE(COUNTA(A2:A)). In Python, write each row with enumerate(reader, start=1). All three produce a numbered column; only the Python route stays correct when fields contain line breaks.
Why does my line count not match the number of rows?
Because a quoted field can contain line breaks under RFC 4180, so one record may span several lines. Count with a CSV parser, not with wc -l.
Should numbering start at 1?
Number data rows from 1 and leave the header unnumbered, or label the column row_no. A header reading 0 in the first row causes off-by-one joins later.
Is a row number the same as an ID?
No. A row number is positional and changes on sort; an ID must survive re-sorting. Use a counter for reading, an ID for joining.
Count Records, Not Newlines
Drop the file in and get the real record count, the field count per row, and every row where the two disagree.
Related reading
File Operations — other guides that pair well with this one.
- Sort CSV by Column
- microsoft-outage-export-your-data
- CSV Column Mixed Types: Why pandas Turns Numbers Into Text
- How to Compress a CSV File
Browse all guides in the NoCodeCSV blog.