Skip to content
1 hidden cell
Introduction to Importing Data in Python
Introduction to Importing Data in Python
Run the hidden code cell below to import the data used in this course.
1 hidden cell
Take Notes
Add notes about the concepts you've learned and code cells with code you want to keep.
Add your notes here
Explore Datasets
Try importing the remaining files to explore the data and practice your skills!
datasets/disarea.dta
datasets/ja_data2.mat
datasets/L-L1_LOSC_4_V1-1126259446-32.hdf5
datasets/mnist_kaggle_some_rows.csv
datasets/sales.sas7bdat
Exploring current working directory in Py
import os
wd = os.getcwd()
os.listdir(wd)
Opening a pickel file
# Import pickle package
import pickle
# Open pickle file and load data: d
with open('data.pkl', 'rb') as file:
d = pickle.load(file)
# Print d
print(d)
# Print datatype of d
print(type(d))
Working with Excel
# Import pandas
import pandas as pd
# Assign spreadsheet filename: file
file = 'battledeath.xlsx'
# Load spreadsheet: xls
xls = pd.ExcelFile(file)
# Print sheet names
print(xls.sheet_names)
# Load a sheet into a DataFrame by name: df1
df1 = xls.parse('2004')
# Load a sheet into a DataFrame by index: df2
df2 =xls.parse(0)
# Parse the first sheet and rename the columns: df1
df1 = xls.parse(0, skiprows=[0], names= ['Country', 'AAM due to War (2002)'])
# Print the head of the DataFrame df1
print(df1.head())
# Parse the first column of the second sheet and rename the column: df2
df2 = xls.parse(1, usecols=[0], skiprows=[0], names=['Country'])
# Print the head of the DataFrame df2
print(df2.head())
SAS files
# Import sas7bdat package
from sas7bdat import SAS7BDAT
# Save file to a DataFrame: df_sas
with SAS7BDAT('sales.sas7bdat') as file:
df_sas = file.to_data_frame()
Stata files