Importing Various CSV Datasource in Python

From previous post (https://paparadit.blogspot.com/2020/11/python-common-libraries.html), we learn how to deal with libraries in Python. On this post, I'm going to show you how to import CSV data using Python. I'll update this post as soon as I discover another way how to do it:

1. CSV from local computer

import pandas as pd
from google.colab import files
uploaded = files.upload()
import io
vlog96 = pd.read_csv(io.BytesIO(uploaded['vlog96.csv']))
vlog96.head()

2. CSV from GitHub

import pandas as pd
url = 'https://raw.githubusercontent.com/kokocamp/vlog96/master/vlog96.csv'
vlog96 = pd.read_csv(url)
vlog96.head()

You can see my video below to make a practice on how to import CSV files from local computer and GitHub.


3. CSV from Google Drive (#1)

The long way:

 import pandas as pd

# Code to read csv file into Colaboratory:
!pip install -U -q PyDrive
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials

# Authenticate and create the PyDrive client.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)

link = 'https://drive.google.com/open?id=1-2lhLcwe9QNT8okL69QL_tV-O4BN9um6'
fluff, id = link.split('=')
print (id)
downloaded = drive.CreateFile({'id':id})
downloaded.GetContentFile('vlog98.csv')
vlog98 = pd.read_csv('vlog98.csv')
vlog98.head()

4. CSV from Google Drive (#2)

The simple way: 

import pandas as pd

from google.colab import drive
drive.mount('/content/drive')

path = '/content/drive/My Drive/data/vlog96.csv'
vlog96 = pd.read_csv(path)
vlog96.head()

I wrapped both way on a video below: 


For GitHub resource, you can use CSV from my account (https://github.com/kokocamp) on or you can make it your own.

Labels: ,


PS: If you've benefit from this blog,
you can support it by making a small contribution.

Enter your email address to receive feed update from this blog:

Post a Comment

 

Post a Comment

Leave comments here...