Importing and Exporting datasets to and from a Google Colab -‘Python’.
The first step to working on any datasets from external sources either for analysis or data science is to make sure that the dataset is loaded/imported to your notebook.
This process is very critical to any first step on working on data. And this article will quickly take you though some few procedures of loading your dataset to a Google Colab Notebook.
- a. Loading a csv file to a Pandas Dataframe.
To simply do this;
#importing pandas library
import pandas as pd#read data from a file with path ('filename.csv')
df= pd.read_csv('filename.csv')#to view the first 10 rows of the dataset
b. Exporting a csv file.
#In this case we use fuction 'to_csv' and the filename will be what you want the name of your file to be.df.to_csv('filename.csv', index = False)
2. a. Loading an excel file to a Pandas Dataframe.
#Take note the filename is the name of the excel file
#make sure the sheetname has no underscoredf = pd.read_excel(‘filename.xlsx’, sheetname= ‘sheet1’)
b. Data into an excel file.
#We'll use an example of data 'x'
#change it into a data frame before exporting it
#This time sheet_name has an underscoredf = pd.DataFrame(x)
df.to_excel('filename.xlsx', sheet_name = 'New_sheet')
3. Creating a dataframe from a dataset with a url source.
#When trying to import a dataset from a url source just copy the url
then you'll use that as the source of the data data = "url"#to read the url use function read_csv
df = pd.read_csv(data)#To view the first 10 rows
4. Importing files from a local drive to a google colab.
This last one is interesting since it prompts the user to choose the files themeselves.
#import packages to use
from google.colab import files
data = files.upload()
Once you write the above code you are prompted to choose the file from your local drive.
#IO enables python's facilities to deal with various i/o types
df = pd.read_csv(io.BytesIO(data['filename.csv']))