We use read_csv to open and load data from CSV files into a table format. This helps us work with data easily in Python.
0
0
Reading CSV files (read_csv) in Data Analysis Python
Introduction
You have a list of sales data saved as a CSV file and want to analyze it.
You downloaded a dataset from the internet in CSV format and want to explore it.
You want to combine data from multiple CSV files for a project.
You need to clean or transform data stored in a CSV before using it.
You want to quickly check the contents of a CSV file without opening it manually.
Syntax
Data Analysis Python
import pandas as pd df = pd.read_csv('filename.csv')
The filename.csv should be the path to your CSV file.
This command loads the CSV data into a DataFrame called df, which is like a table.
Examples
Loads a CSV file named
data.csv from the current folder.Data Analysis Python
import pandas as pd df = pd.read_csv('data.csv')
Loads a CSV file from a subfolder called
folder.Data Analysis Python
import pandas as pd df = pd.read_csv('folder/sales.csv')
Loads a CSV file where columns are separated by semicolons instead of commas.
Data Analysis Python
import pandas as pd df = pd.read_csv('data.csv', sep=';')
Sample Program
This program reads a CSV file named sample_data.csv and prints the first 3 rows to see the data.
Data Analysis Python
import pandas as pd # Load CSV file into DataFrame df = pd.read_csv('sample_data.csv') # Show first 3 rows print(df.head(3))
OutputSuccess
Important Notes
If the file is not in the same folder as your script, provide the full or relative path.
You can use df.head() to see the first few rows and check the data.
If your CSV uses a different separator, use the sep parameter to specify it.
Summary
read_csv loads CSV files into a DataFrame for easy data work.
Always check your file path and separator to avoid errors.
Use head() to quickly preview your data after loading.