Different Ways to Import CSV File into Pandas

Let`s see how you can import a CSV file into Pandas.

Method # 1: Using the read_csv () method.

# pandas module import

import pandas as pd 

 
# create data frame

df = pd.read_csv ( " https://media.python.engineering/wp-content/uploads /nba.csv "

  

df.head ( 10

Output:

Provide file_path .

# import pandas as pd

import pandas as pd

 
# Accepts file folder

filepath = r "C: Gfgdatasets ba.csv"

  
# read CSV file

df = pd.read_csv (filepath)

 
# print first five lines

print (df.head ())

Output:

Method # 2: Using the csv module.

You can directly import the csv files using the module csv .

# import the module into csv

import csv

import pandas as pd

 
# open csv file

with open (r " C: UsersAdminDownloads ba.csv " ) as csv_file: 

 

# read the csv file

csv_reader = csv.reader (csv_file, delimiter = ` , ` )

  

# We can now use these CSV files in pandas

df = pd.DataFrame ([csv_reader], index = None )

  df.head ()

  
# repeating the values ​​of the first column

for val in list (df [ 1 ]):

print (val)

Output: