Different ways to import csv file in Pandas
CSV files are the “comma separated values”, these values are separated by commas, this file can be viewed as an 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 be in CSV file format. Let’s see the different ways to import csv files in Pandas.
Ways to Import CSV File in Pandas
There are various ways to import CSV files in Pandas, here we are discussing some generally used methods for importing CSV files in pandas.
- Using read_csv() Method
- Using
csv
- Using numpy Module
Import a CSV File into Python using Pandas
In this method the below code uses the panda’s library to read an NBA-related CSV file from a given URL, creating a DataFrame named `df`. It then prints the first 10 rows of the DataFrame to provide a brief overview of the dataset.
Python3
# importing pandas module import pandas as pd # making data frame df = pd.read_csv("https: / / media.w3wiki.net / wp - content / uploads / nba.csv") df.head( 10 ) |
Output:
Providing file_path
In this example the below code uses the pandas library to read a CSV file (“C:\Gfg\datasets\nba.csv”) into a DataFrame and then prints the first five rows of the DataFrame.
Python3
# import pandas as pd import pandas as pd # Takes the file's folder filepath = r "C:\Gfg\datasets\nba.csv" ; # read the CSV file df = pd.read_csv(filepath) # print the first five rows print (df.head()) |
Output:
Import CSV file in Pandas using csv
module.
One can directly import the csv files using csv
module. In this code example the below code reads a CSV file (“nba.csv”) into a Pandas DataFrame using Python’s `csv` and `pandas` modules. It then prints the values in the first column of the DataFrame. A correction is needed in the DataFrame creation line for accurate functionality.
Python3
# import the module csv import csv import pandas as pd # open the csv file with 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 column for val in list (df[ 1 ]): print (val) |
Output:
Loading CSV Data into a NumPy Array
Way to import a CSV file in Python is by using the numpy
library. The numpy
library provides the genfromtxt()
function, which can be used to read data from a CSV file and create a NumPy array.
Example : Replace '
path/to/your/file.csv
'
with the actual path to your CSV file. The delimiter=','
parameter indicates that the values in the CSV file are separated by commas. This method is useful when you want to work with numerical data and leverage the capabilities of the NumPy library.
Python3
import numpy as np # Specify the path to the CSV file csv_file_path = 'path/to/your/file.csv' # Use genfromtxt to read the CSV file into a NumPy array data_array = np.genfromtxt(csv_file_path, delimiter = ',' ) # Now, data_array contains the data from the CSV file print (data_array) |
Output :
[[1. 2. 3.]
[4. 5. 6.]
[7. 8. 9.]]
This is a hypothetical example assuming the CSV file contains a 3×3 matrix of numerical values separated by commas.
Contact Us