Lesson 2. Import Text Data Into Numpy Arrays

In This Lesson

In this lesson, you will write Python code in Jupyter Notebook to import text data (.txt. and .csv files) into numpy arrays. You will also write Python to download the datasets (.txt. and .csv files) needed for the numpy array lessons.

What You Need

Be sure that you have a subdirectory called data under your earth-analytics-bootcamp directory. For help with this task, please see the challenge for the lesson on Intro to Shell.

The code below is available in the ea-bootcamp-day-4 repository that you cloned to earth-analytics-bootcamp under your home directory.

Text Files

Scientific data can come in a variety of file formats and types. In this course, you will work with data stored in plain text files (.txt) and comma-delimited text files (.csv).

Plain Text Files

Plain text files simply list out the values on separate lines without any symbols or delimiters to indicate separate values. For example, data for the average monthly precipitation data for Boulder, CO can be stored as a plain text file (.txt), with a separate line for each month’s value.

0.700.751.852.933.052.021.931.621.841.311.390.84

Due to their simplicity, text files (.txt) can be very useful for collecting very large datasets that are all the same type of observation or data type.

CSV Files

Unlike plain-text files which simply list out the values on separate lines without any symbols or delimiters, comma delimited (CSV) files use commas (or some other delimiter like tab spaces or semi-colons) to indicate separate values.

This means that CSV files can easily support multiple rows and columns of related data. For example, data for the monthly precipitation for Boulder, CO for the years 2002 and 2013 can be stored together in a comma delimited (.csv) file.

Set Working Directory

Remember that you can check the current working directory using os.getcwd(). You can also set the current working directory using another useful function os.chdir().

# set the working directory to the `earth-analytics-bootcamp` directory# replace `jpalomino` with your username here and all paths in this lessonos.chdir("/home/jpalomino/earth-analytics-bootcamp/")# print the current working directoryos.getcwd()

'/home/jpalomino/earth-analytics-bootcamp'

Download Data Using URLs

You can use the urllib package to download data from online sources such as Figshare.com, where the datasets for this lesson are published.

To use urllib, you provide parameter values for url as well as filename for the downloaded file.

For this lesson, you will download the .txt files for average monthly precipitation for Boulder, CO as well as the month names from the Earth Lab Figshare.com repository.

Note that you do not have to provide the full path for filename because it is relative to the current working directory that you set using os.chdir().

Into which directory were your files downloaded? Open the directory on your computer to see your downloaded files.

Import Text Data Into Numpy Arrays

Numeric Data

You can create new numpy arrays by importing data from files, such as text files. You can import these data using the loadtxt() function from numpy, which you imported as np.

For both .txt and .csv files, you need to specify a value for the parameter called fname for the file name (e.g. np.loadtxt(fname = "filename.txt")). Be sure to update the path for the file to your home directory.

# import the monthly average values from `avg-monthly-precip.txt` as a numpy arrayavg_monthly_precip=np.loadtxt(fname="/home/jpalomino/earth-analytics-bootcamp/data/avg-monthly-precip.txt")

Recall that that you can use the print() function to see the values stored in a variable (e.g. print(variablename)).

# print the data in `avg_monthly_precip`print(avg_monthly_precip)

[0.7 0.75 1.85 2.93 3.05 2.02 1.93 1.62 1.84 1.31 1.39 0.84]

You can also use the type() function to check the type of data structure (e.g. type(variablename)) and see that avg_monthly_precip is a numpy array.

# print the type for the `avg_monthly_precip` variableprint(type(avg_monthly_precip))

<class 'numpy.ndarray'>

Text String Data

In addition to numeric data, you can also import text strings to numpy arrays using the genfromtxt() function from numpy. You need to specify a parameter value for filename as well as for the data type as dtype='str'.

# import the names of the months from month.txt as a numpy arraymonths=np.genfromtxt("/home/jpalomino/earth-analytics-bootcamp/data/months.txt",dtype='str')

Again, you can check the type and the data in your new numpy array.

# print the type for the `months` variableprint(type(months))# print the values in `months`print(months)

Congratulations! You have now learned how to create numpy arrays by importing numeric and text string data from text files.

Optional Challenge

Test your Python skills to:

Download a .csv file containing the monthly precipitation for Boulder, CO for the years 2002 and 2013 (monthly-precip-2002-2013.csv)from https://ndownloader.figshare.com/files/12707792.

Be sure to assign a useful variable name that is short but indicative of what it contains (e.g. precip_2002_2013).

Import the data from this .csv file into a numpy array.

Note that for .csv files, you need to specify another parameter in addition to filename. You need to provide a value for delimiter, which indicates the symbol that is used to separate the values (e.g. delimiter = ",").

Print the data type of your new numpy array as well as its contents.

Note that you can add a line of code before print(variablename) to display the values in the numpy array as floats, rather than scientific notation (np.set_printoptions(suppress=True)).

Print the data contained in avg_monthly_precip, and compare it to your new numpy array. Do you notice any differences in the structure of the data between these two numpy arrays?