Reading CSV with options (sep, header, encoding)
📖 Scenario: You have a CSV file from a friend who uses semicolons ; instead of commas to separate values. Also, the file has no header row, and it uses a special text encoding called latin1. You want to read this file correctly into a table so you can analyze it.
🎯 Goal: Learn how to read a CSV file using pandas.read_csv with options for separator, header, and encoding to get the data loaded properly.
📋 What You'll Learn
Create a variable called
file_path with the exact string 'data/friends_data.csv'.Create a variable called
separator and set it to the string ';'.Create a variable called
header_option and set it to None.Create a variable called
file_encoding and set it to the string 'latin1'.Use
pandas.read_csv with the variables file_path, sep=separator, header=header_option, and encoding=file_encoding to read the CSV into a variable called df.Print the first 5 rows of
df using print(df.head()).💡 Why This Matters
🌍 Real World
CSV files come in many formats. Knowing how to adjust reading options helps you load data correctly from different sources.
💼 Career
Data scientists often receive data files with various separators and encodings. Mastering these options is essential for data cleaning and preparation.
Progress0 / 4 steps