Page 211 - Touhpad Ai
P. 211
Importing a CSV file into a DataFrame
This function is versatile and handles various configurations of CSV files, making it straightforward to load data for
analysis or manipulation.
You can import a CSV file into a pandas DataFrame using the read_csv() function. The syntax of the read_csv()
function is:
pd.read_csv("filename.csv")
Where, filename.csv is the name of the file with .csv extension that you want to import.
The pd.read_csv() function in pandas is quite versatile and allows you to specify various parameters to customise
how the CSV file is read. Some commonly used parameters are as follows:
u filepath: The path to the CSV file.
u sep: The delimiter to use for separating columns. These delimiters can be a comma, semicolon, tab, or any other
character. The default value for ‘sep’ is a comma.
u header: It specifies which row to use as column names. ‘header=0’ means the column names are taken from the first
line of the file. By default, ‘header=0’.
u index_col: It specifies which column to use as the row labels.
u dtype: A dictionary where keys are column names and values are data types.
u encoding: It specifies the encoding to use for reading the file.
u compression: It specifies the compression mode for reading compressed files.
Program 19: To import a CSV file into DataFrame
# Importing pandas library
import pandas as pd
# making data frame
#specify path of file in case file is in a different folder
df = pd.read_csv("Customer.csv",sep=',', header=0)
print(df)
Output:
Customer ID First Name Last Name City
0 1 Akash Patel Mumbai
1 2 Priya Sharma Delhi
2 3 Aarav Singh Jaipur
3 4 Neha Trivedi Ahmedabad
4 5 Rahul Mehta Bangalore
5 6 Aishwarya Iyer Chennai
6 7 Rajesh Reddy Hyderabad
7 8 Sneha Gupta Kolkata
8 9 Sanjay Kumar Lucknow
9 10 Deepika Menon Pune
Theoretical and Practical Aspects of Data Processing 209

