• Offers
    • RegisterLogin
      • Learn More
    PythonPoint.netPythonPoint.net
    • Offers
    • RegisterLogin
      • Learn More

      Python

      SKILL IS IMPORTANT THAN DEGREE Be skill full.
      • Home
      • Blog
      • Python
      • How to Import Dataset in Python

      How to Import Dataset in Python

      • Posted by Python Point Team
      • Categories Python
      • Date November 30, 2020
      • Comments 0 comment
      how to import dataset in python

      While working in python programs, sometimes we need dataset for data analysis. Python has various modules that help us in importing the external data in various file formats to a python program. In this article, we will see how to import data of various formats to a python program.

      • Import CSV file
      • Read Excel File
      • Read Text File
      • Read SQL Table

      In this article we will be using pandas. pandas is a powerful data analysis package. It makes data exploration and manipulation easy. It has several functions to read data from various sources. If pandas is not installed in your system, use the following command to install pandas

      pip install pandas

      Import CSV file

      The csv module enables to read each of the row in the file using a comma as a delimiter.

      import csv
      
      with open("Students.csv", 'r') as file:
      	rows = csv.reader(file, delimiter = ',')
      	for r in rows:
      		print(r)

      Output:

      ['Name', 'Age', 'Grade']
      ['ABEL', '20', 'A']
      ['BINDU', '21', 'A']
      ['CHRISTY', '20', 'A']
      ['YOUSUF', '21', 'A']
      ['KRISHNA', '20', 'A']

      Read Excel File

      The read_excel() function in pandas library can be used to import excel data into Python.

      import pandas as pd 
      
      ds = pd.read_excel("Students.xlsx")
      
      print(ds)

      Output:

            Name  Age Grade
      0     ABEL   20     A
      1    BINDU   21     A
      2  CHRISTY   20     A
      3   YOUSUF   21     A
      4  KRISHNA   20     A

      Read Text File

      The read_table() function is used to pull data from text file.

      import pandas as pd 
      
      ds = pd.read_table("Students.txt")
      print(ds)

      Output:

            Name  Age Grade
      0     ABEL   20     A
      1    BINDU   21     A
      2  CHRISTY   20     A
      3   YOUSUF   21     A
      4  KRISHNA   20     A

      Read SQL Table

      Using pyodb module, we can connect to database servers. This will help us import data from relational sources using a sql query

      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()

      You have to provide valid information like driver details, server, username, password, database in the above piece of code. By passing the required query in the ‘SQL QUERY’ , you will get the corresponding output.

      • Share:
      author avatar
      Python Point Team

      Previous post

      How to Normalize Data in Python
      November 30, 2020

      Next post

      How to declare an array in Python
      November 30, 2020

      You may also like

      Introduction to Transfer Learning with Python: A Practical Guide
      31 December, 2022

      Introduction: Definition of transfer learning Overview of how transfer learning works in the context of machine learning Why transfer learning is useful and important Section 1: Transfer learning in Python with Keras In this section, we will explore how to …

      How to Check Type in Python
      31 December, 2022

      In this article, we will learn to check type in Python. The built-in function type() can be used to check the type of data in Python.

      How to make web crawler in python?
      31 December, 2022

      A web crawler is a systematic, well-defined process of extracting specific data about a topic. We have a library named as scrapy which helps in making web crawler. We can install scrapy by the following command:- Now we can easily …

      Subscribe
      Login
      Notify of
      Please login to comment
      0 Discussion
      Inline Feedbacks
      View all comments

      Latest Courses

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      (Hindi) Ways to earn minimum 1 Lakh Per month as Programmer

      ₹10,000
      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      (HINDI) Full Stack Web Development In Python 3.8 And Django 3.1

      ₹25,000 ₹2,500

      Latest Posts

      • Introduction to Transfer Learning with Python: A Practical Guide
      • How to Check Type in Python
      • How to make web crawler in python?
      • Why was the language called “python”?
      • Why Python is so Popular
      Contact
      •   support@pythonpoint.com

      We get you the best Python Courses and Blogs aiming to provide skill.

      We Believe Skill is much more important than a Degree

      Company
      • About Us
      • Blog
      • Offers
      • Contact
      Useful Links
      • Courses
      Support
      • Need Support

      © 2020 ALL RIGHTS RESERVED​ PYTHONPOINT.NET

      PythonPoint

      • Terms of Use
      • Refund Policy
      • Privacy Policy

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now

      wpDiscuz