Working with CSV and Excel Files
After completing this topic, you will be able to:
- Read and write CSV and Excel files using pandas.
- Solve common issues encountered in real-world scenarios, such as encoding, sheet selection, and column specification.
What is CSV?
CSV (Comma-Separated Values) is a text file where values are separated by commas. When opened in a text editor, it looks like this:
Name,Age,City
Kim Hoon,30,Seoul
Lee Soo,25,Busan
Park Jin,35,DaejeonIt is the most common data exchange format. It can be read by any programming language and any operating system. Excel, Google Sheets, and databases all support CSV export.
Reading CSV Files
import pandas as pd
# Basic readingdf = pd.read_csv("data.csv")
# Specifying encoding - preventing Korean character errorsdf = pd.read_csv("data.csv", encoding="cp949")# Korean CSV files saved from Windows Excel use cp949 (or euc-kr)
# UTF-8 BOMdf = pd.read_csv("data.csv", encoding="utf-8-sig")When reading a Korean CSV file and encountering a UnicodeDecodeError, it's an encoding issue. Try the following in order: utf-8 β utf-8-sig β cp949 β euc-kr.
Reading CSV Files - Advanced Options
# Reading only specific columns - saving memorydf = pd.read_csv("data.csv", usecols=["Name", "Age"])
# When the delimiter is not a commadf = pd.read_csv("data.tsv", sep="\t") # Tab-delimiteddf = pd.read_csv("data.txt", sep="|") # Pipe-delimited
# File with no headerdf = pd.read_csv("data.csv", header=None, names=["col1", "col2", "col3"])
# Specifying rows to skipdf = pd.read_csv("data.csv", skiprows=2) # Skips the first 2 rows
# Large file - reading in chunksfor chunk in pd.read_csv("big.csv", chunksize=10000): process(chunk) # Processes 10,000 rows at a timechunksize prevents memory errors in large files (hundreds of MB or more). Instead of loading the entire file into memory at once, it processes it in chunks.
Reading Excel Files
# Basic reading (first sheet)df = pd.read_excel("report.xlsx")
# Specific sheetdf = pd.read_excel("report.xlsx", sheet_name="Sales")
# Reading multiple sheets at oncesheets = pd.read_excel("report.xlsx", sheet_name=None)# {'Sheet1': DataFrame, 'Sales': DataFrame, 'Costs': DataFrame}
for name, data in sheets.items(): print(f"{name}: {len(data)} rows")
# Specifying cell rangedf = pd.read_excel("report.xlsx", usecols="B:E", # Only columns B to E skiprows=3, # Skips the first 3 rows nrows=100) # Only 100 rowsReading Excel files requires the openpyxl package (pip install openpyxl).
Saving Files
# Saving to CSVdf.to_csv("output.csv", index=False, encoding="utf-8-sig")# index=False: Excludes the row number column# utf-8-sig: Prevents Korean characters from being garbled when opened in Excel
# Saving to Exceldf.to_excel("output.xlsx", index=False, sheet_name="Results")
# Saving to multiple sheetswith pd.ExcelWriter("output.xlsx") as writer: df_sales.to_excel(writer, sheet_name="Sales", index=False) df_cost.to_excel(writer, sheet_name="Costs", index=False)When saving to CSV, using encoding="utf-8-sig" will prevent Korean characters from being garbled when opened in Windows Excel. Saving with utf-8 often causes garbled characters in Excel.
Real-World Workflow - Merging CSV Files
Merging multiple monthly CSV files into one is a very common task.
import glob
# Find all CSV files in the folderfiles = glob.glob("monthly_data/*.csv")print(f"{len(files)} files found")
# Read and mergedfs = []for f in sorted(files): temp = pd.read_csv(f, encoding="utf-8-sig") temp["source_file"] = f # Record the original file name dfs.append(temp)
df_all = pd.concat(dfs, ignore_index=True)print(f"Total {len(df_all)} rows")
# Save the resultdf_all.to_csv("merged_all.csv", index=False, encoding="utf-8-sig")If you don't include ignore_index=True, the indexes of each file will be preserved, resulting in an index like 0, 1, 2, ..., 0, 1, 2, ...
Key Takeaways
CSV and Excel are the entry and exit points of data analysis. You often receive data from external sources in CSV/Excel format, and you often deliver results in CSV/Excel format. If you are confident in encoding (utf-8-sig vs cp949), sheet selection, and column specification, you can solve 90% of "file won't open" problems.