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
# Add your code snippets 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
Reading a Text file
filename = 'name.txt' file = open(filename, mode='r') #'r' is to read. if u want to write, use argument 'w' instead of 'r' text = file.read() file.close() print(text)
Context Manager with
with open(filename, mode='r') as file print(file.read())
Flat files
Import numpy as np filename = 'name.txt' data = np.loadtxt(filename, delimeter=',', skiprows=1, usecols=[0, 2]) print(data)
Working with mixed datatypes
np.genfromtxt() data = np.genfromtxt('titanic.csv', delimiter=',', names=True, dtype=None)
or
np.recfromcsv()
Assign the filename: file
file = 'titanic.csv' d = np.recfromcsv(file, delimiter=',', names=True, dtype=None) print(d[:3])
Importing flat files using pandas
import pandas as pd file = 'titanic.csv' df = pd.read_csv(file) print(df.head())