How to Read CSV Files in Pandas: Simple Guide
Use the
pandas.read_csv() function to load a CSV file into a DataFrame. Provide the file path as a string argument, and pandas will parse the CSV data into a table you can work with.Syntax
The basic syntax to read a CSV file in pandas is:
pandas.read_csv(filepath, sep=',', header='infer', names=None, index_col=None)- filepath: Path to the CSV file as a string.
- sep: Delimiter used in the file, default is comma.
- header: Row number to use as column names, default is to infer from first row.
- names: List of column names to use if no header row.
- index_col: Column(s) to set as index of the DataFrame.
python
import pandas as pd df = pd.read_csv('file.csv')
Example
This example shows how to read a CSV file named data.csv into a pandas DataFrame and display its first few rows.
python
import pandas as pd # Create a sample CSV file csv_content = '''name,age,city Alice,30,New York Bob,25,Los Angeles Charlie,35,Chicago''' with open('data.csv', 'w') as f: f.write(csv_content) # Read the CSV file df = pd.read_csv('data.csv') # Show the DataFrame print(df)
Output
name age city
0 Alice 30 New York
1 Bob 25 Los Angeles
2 Charlie 35 Chicago
Common Pitfalls
Common mistakes when reading CSV files include:
- Wrong file path causing
FileNotFoundError. - Incorrect delimiter if the file uses tabs or semicolons instead of commas.
- Missing header row leading to wrong column names.
- Not specifying encoding for files with special characters.
Always check the file format and adjust parameters accordingly.
python
import pandas as pd # Wrong delimiter example (semicolon instead of comma) # This will cause incorrect parsing # df_wrong = pd.read_csv('data.csv', sep=';') # Correct way if delimiter is semicolon # df_correct = pd.read_csv('data.csv', sep=';')
Quick Reference
Here is a quick cheat sheet for read_csv parameters:
| Parameter | Description | Default |
|---|---|---|
| filepath | Path to the CSV file | None (required) |
| sep | Delimiter character | ',' |
| header | Row number for column names | 'infer' (first row) |
| names | List of column names | None |
| index_col | Column(s) to use as index | None |
| encoding | File encoding | 'utf-8' |
| skiprows | Rows to skip at start | None |
| na_values | Additional strings to recognize as NA | None |
Key Takeaways
Use pandas.read_csv() with the file path to load CSV data into a DataFrame.
Check the delimiter and header settings to match your CSV file format.
Handle file paths and encodings carefully to avoid errors.
Use parameters like index_col and names to customize the DataFrame structure.
Always preview your data after loading to confirm it was read correctly.