5 Different Ways to Load Data in Python



As a beginner, you might only know a single way to load data (normally in CSV) which is to read it using pandas.read_csv function. It is one of the most mature and strong functions, but other ways are a lot helpful and will definitely come in handy sometimes.


The ways that I am going to discuss are:

  • Manual function

  • loadtxt function

  • genfromtxtf unction

  • read_csv function

  • Pickle

The dataset that we are going to use to load data can be found here. It is named as 100-Sales-Records.


Imports

We will use Numpy, Pandas, and Pickle packages so import them.

import numpy as np 
import pandas as pd 
import pickle  

1. Manual Function

This is the most difficult, as you have to design a custom function, which can load data for you. You have to deal with Python’s normal filing concepts and using that you have to read a .csv file.

Let’s do that on 100 Sales Records file.


def load_csv(filepath):     
    data =  []     
    col = []     
    checkcol = False     
    with open(filepath) as f:         
        for val in f.readlines():             
            val