Login
📚 Python for Introductory Statistics
Chapters ▾

14.2 Uploading Dataset

Uploading Excel or csv Files from Your Computer

Watch demo video

If your data file is in Excel spreadsheet or csv (comma separated value) format stored in your local computer, you can run the code-cell below to upload the file in to Google Colab.

from google.colab import files
uploaded = files.upload()

After running the code, click the Choose Files button and browse your computer's folder to locate your Excel or csv file. Open the file. Colab will upload it with the same file name, for example file_name.xlsx. Next, you will need to run the code-cell below to convert it to pandas dataframe.

import io
import pandas as pd

excel_file = io.BytesIO(uploaded['file_name.xlsx'])
x_df = pd.read_excel(excel_file)

Make sure the file names match. Now your dataframe, x_df, is ready for analysis and plotting.

For a csv file run the code-cell below

import io
import pandas as pd

csv_file = io.StringIO(uploaded['MyData.csv'].decode('utf-8'))
x_df = pd.read_csv(csv_file)

Uploading csv Dataset From URL

Watch demo video

If you want to upload a csv file from the internet with url address, use the code below. Replace the web_link_address with your url address first.

import pandas as pd
url = `www_link_address`

x_df = pd.read_csv(url)

Example: Write a program to read the dataset (URL below) and display the shape (size) of the dataset. In addition, display the first and last five rows of the dataset.

my_csv_file.csv

import pandas as pd

url = 'my_csv_file.csv'

x_df = pd.read_csv(url)
x_df.shape   # displays the number of rows and columns
Show expected output
(180, 8)

Interpretation: There are 180 rows and 8 columns in x_df.

x_df   # displays the frist 5 and the last 5 rows
idgendersectiontimeleveltest1_scoretest2_scoretest3_score
01femaleC17.3low65.484.351.9
12femaleD5.1low72.486.152.0
23femaleA2.4low73.884.253.1
34femaleA8.8low70.287.952.0
45maleB15.0low60.381.455.5
...........................
175176maleB13.2high78.889.352.5
176177femaleB12.6high76.583.450.0
177178maleD8.4high78.186.351.0
178179maleC9.5high76.686.652.4
179180maleC16.2high83.189.454.1

Cleaning Data

Common cleaning data techniques include removing duplicate columns or observations (rows), combining or summarizing columns and replacing or deleting missing values.

  1. To drop a column use

x_df.drop(['column_name'],axis=1,inplace=True)

  1. To drop duplcate rows by keeping only the first observation, use

x_df.drop_duplicates(subset='column_name', keep="first",inplace=True)

  1. To combine columns and make a new one, for example average use

df['average']=(x_df['test1']+x_df['test2]+x_df['test3])/3

  1. To drop the rows where at least one element is missing, use

x_df.dropna

  1. To drop the columns where at least one element is missing, use

x_df.dropna(axis='columns')

  1. x_df.isnull.sum returns the number of missing values for each column
  2. To replace missing (NaN) values with the mean, use

x_df['column_name'].fillna(value=df['column_name'].mean, inplace=True)

Downloading Data as csv file to your computer

If you make changes to your x_df in Google colab and want to save or download your updated x_df to your local computer you can follow the steps below. For more on updating your dataframe, see the Cleaning Data section above.

first run the code-cell below

from google.colab import drive

then on a new code-cell run

drive.mount('/drive')

You will receive a message that says go to the url link to get authorization from your Google account. Click the link, then choose your Google Account (if you have more than one), then scroll down and click allow. Next you will need to copy the authorization code. Then go back to your Colab, paste the code where it says Enter Authorization Code and press enter.

You will see a new message: Mounted at /drive

Now you will need to choose a folder in your Google Drive where the file is to be downloaded or saved. For example:

x_df.to_csv('/drive/My Drive/Yiakl/my_csv_file.csv')

your dataframe name is x_df and your file will be downloaded to your Google Drive under the Yiakl folder. The name of the downloaded file is chosen here to be my_csv_file.csv.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.