Importing Data in Python


When running python programs, we need to use datasets for data analysis. Python has various modules which help us in importing the external data in various file formats to a python program. In this example we will see how to import data of various formats to a python program.

Import csv file

The csv module enables us to read each of the row in the file using a comma as a delimiter. We first open the file in read only mode and then assign the delimiter. Finally use a for loop to read each row from the csv file.

Example

import csv

with open("E:\customers.csv",'r') as custfile:
rows=csv.reader(custfile,delimiter=',')
for r in rows:
print(r)

Output

Running the above code gives us the following result −

['customerID', 'gender', 'Contract', 'PaperlessBilling', 'Churn']
['7590-VHVEG', 'Female', 'Month-to-month', 'Yes', 'No']
['5575-GNVDE', 'Male', 'One year', 'No', 'No']
['3668-QPYBK', 'Male', 'Month-to-month', 'Yes', 'Yes']
['7795-CFOCW', 'Male', 'One year', 'No', 'No']
……
…….

With pandas

The pandas library can actually handle most of the file types inclusing csv file. In this program let see how pandas library handles the excel file using the read_excel module. In the below example we read the excel version of the above file and get the same result when we read the file.

Example

import pandas as pd

df = pd.ExcelFile("E:\customers.xlsx")
data=df.parse("customers")
print(data.head(10))

Output

Running the above code gives us the following result −

  customerID   gender Contract            PaperlessBilling Churn
0 7590-VHVEG Female   Month-to-month         Yes            No
1 5575-GNVDE   Male   One year                No            No
2 3668-QPYBK   Male   Month-to-month         Yes           Yes
3 7795-CFOCW   Male   One year                No            No
4 9237-HQITU Female   Month-to-month        Yes            Yes
5 9305-CDSKC Female   Month-to-month        Yes            Yes
6 1452-KIOVK   Male   Month-to-month        Yes             No
7 6713-OKOMC Female   Month-to-month         No             No
8 7892-POOKP Female   Month-to-month        Yes            Yes
9 6388-TABGU   Male   One year               No             No

With pyodbc

We can also connect to database servers using a module called pyodbc. This will help us import data from relational sources using a sql query. Ofcourse we also have to define the connection details to the db before passing on the query.

Example

import pyodbc
sql_conn = pyodbc.connect("Driver={SQL Server};Server=serverName;UID=UserName;PWD=Password;Database=sqldb;")
data_sql = pd.read_sql_query(SQL QUERY’, sql_conn)
data_sql.head()

Output

Depending the SQL query the result will be displayed.

Updated on: 10-Jul-2020

14K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements