We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Loading Data

We've set up a few small example DataFrames in code, but real data usually comes from files, which, as you know, need to be loaded.

For CSV data, Pandas offers the pd.read_csv() function function, which loads a CSV file directly into a DataFrame:

df = pd.read_csv("stream_history.csv")

That's it! One line of code loads your entire dataset into a convenient form in memory.

Pandas is also smart enough to use the first row of the CSV as column headers, so you don't have to worry about that (as long as the file is well-formed).

Read Row by Row

Sometimes reading a massive file into memory isn't what you want. Maybe you're on a machine with limited RAM, and you're trying to work with a 25GB CSV file. The chunksize parameter lets you read the file in smaller DataFrame chunks:

# will read 100 lines into a DataFrame at a time
chunk_size_num_lines = 100

for chunk in pd.read_csv("large_file.csv", chunksize=chunk_size_num_lines):
    # do stuff with each dataframe...
    process_chunk(chunk)

Assignment

SnackStack's kitchen sensors export daily readings to CSV files. We need to get them imported as Pandas DataFrames for analysis.

Complete the load_device_data function. It accepts a CSV file path and returns a DataFrame with the file's contents.