CSV files are the “comma separated values”, these values are separated by commas, this file can be view like as excel file. In Python, Pandas is the most important library coming to data science. We need to deal with huge datasets while analyzing the data, which usually can get in CSV file format.
Let’s see the different ways to import csv file in Pandas.
Method #1: Using read_csv() method.
# importing pandas module import pandas as pd # making data frame df.head(10) |
Output:
Providing file_path.
# import pandas as pdimport pandas as pd # Takes the file's folderfilepath = r"C:\Gfg\datasets\nba.csv" # read the CSV filedf = pd.read_csv(filepath) # print the first five rowsprint(df.head()) |
Output:
Method #2: Using csv module.
One can directly import the csv files using csv module.
# import the module csvimport csvimport pandas as pd # open the csv filewith open(r"C:\Users\Admin\Downloads\nba.csv") as csv_file: # read the csv file csv_reader = csv.reader(csv_file, delimiter=',') # now we can use this csv files into the pandas df = pd.DataFrame([csv_reader], index=None) df.head() # iterating values of first columnfor val in list(df[1]): print(val) |
Output:

