Easy and convenient method to read a dataset from the Google Drive
In the previous article, I displayed how to read a dataset from GitHub and save it locally. I have some projects and its dataset stored in the Google Drive. In this article, we will see how to read a dataset from Google Drive. If you like to store datasets, or you are planning to store datasets in Google Drive as I, but you are not familiar with methods to read your datasets from your Drive. Then please follow the following process in this article.
1. Create a Share Link
First, we should create a shared link for the dataset. Go to the dataset that you want to share, and right-click to choose the
Share from the menu.
Anyone with the link for the General access.
For the role, we choose
Viewer or others according to your aim.
Then, copy the link and paste in any text file.
2. Open Python IDE
Open any Python IDE, here I use the Jupyter note to display the reading process.
(1) install Pandas library
If you have not installed Pandas, just type the following command in a
command-line shell or in the Jupyter notebook.
pip install pandas
(2) import pandas
import pandas as pd
(3) create url variable
Past the shared link that we copied as the url value.
url = ‘https://drive.google.com/file/d/17LjfBwzFGcv7IHQ0KCLYQavLsR6aEert/view?usp=sharing'
(4) get the data file ID
17LjfBwzFGcv7IHQ0KCLYQavLsR6aEert, but we write a code to use
url.split method to get it, which is the second one from the end.
file_id = url.split('/')[-2]
(5) create the read url
The read or download
url for file in Google Drive is
https://drive.google.com/uc?id=' + file id.
read_url='https://drive.google.com/uc?id=' + file_id
(6) read the data
Finally, we can read the dataset using Pandas methods. In this example,
pd.read_csv is used because this dataset in my Google Drive is in
# read the data
df = pd.read_csv(read_url)
# display the first 5 rows
5. Online Course
If you are interested in learning Python data analysis in details, you are welcome to enroll one of my course: