• 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

      “not in” belongs to which type of operator in python?
      29 December, 2020

      “not in” belongs to the membership operator in python. Membership operator have two operators “in” and “not in”. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary). e.g., …

      What is the difference between Python 2 and 3
      8 December, 2020

      Python 2 and 3 got many differences between them. In this article, we will go through some important differences between them. While comparing these two, it is pretty clear that python 3 is much better than 2. Python 2 is …

      How To Check If A List Is Empty In Python
      7 December, 2020

      In this article, we will see how to check if a list is empty or not. A list is said to be empty when there are no elements in the list. Method 1: Output: Method 2: Output:

      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

      • How to get access to the course after purchase?
      • “not in” belongs to which type of operator in python?
      • What is the difference between Python 2 and 3
      • How To Check If A List Is Empty In Python
      • How to Change Line in Python
      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