Introduction
In this lab, you will learn how to import tabular data from text files using the numpy.genfromtxt function. NumPy (Numerical Python) is a fundamental library for scientific computing in Python that provides powerful data structures and functions for working with numerical data. Its core data structure is the NumPy array - a fast, memory-efficient way to store and manipulate large datasets.
The numpy.genfromtxt function is a cornerstone of data analysis in Python, allowing you to read structured data and convert it into NumPy arrays. We will start with a basic import and progressively add options to handle common real-world scenarios like headers, different column separators, missing values, and selecting specific data columns. All operations will be done by writing and executing Python scripts in the WebIDE.
Basic Data Loading with genfromtxt
First, let's familiarize ourselves with the environment. In the file explorer on the left, you will see two files: main.py and my_data.csv. We will write our Python code in main.py to load data from my_data.csv.
The numpy.genfromtxt function's most basic usage requires one argument: the path to the data source. Let's try to load our data file with the default settings.
Open the main.py file and add the following code to it:
import numpy as np ## This imports NumPy and gives it the alias 'np' for convenience
## Load data from the CSV file
## Relative paths will cause validation to fail, please use absolute paths in the lab
data = np.genfromtxt('/home/labex/project/my_data.csv')
## Print the resulting array
print(data)
Now, save the file and run it from the terminal at the bottom of the IDE.
python main.py
You will see the following output:
[nan nan nan nan]
This output might be surprising. The result is an array of nan (Not a Number) values. NaN is a special floating-point value that represents undefined or unrepresentable numerical results - it's NumPy's way of indicating that a value couldn't be properly converted to a number. This happens because genfromtxt by default tries to split lines by whitespace and interpret everything as a floating-point number. Our file my_data.csv uses commas as separators and contains a non-numeric header line, which causes the default import to fail. In the next step, we will fix this.
Specifying Delimiters and Skipping Headers
To correctly parse our my_data.csv file, we need to tell genfromtxt two things:
- The data is separated by commas.
- The first line is a header and should be ignored.
We can achieve this using the delimiter and skip_header arguments.
delimiter=',': This tells the function to use a comma to separate values.skip_header=1: This tells the function to ignore the first line of the file.
Modify your main.py file with the updated code:
import numpy as np ## Import NumPy library
## Load data, specifying the delimiter and skipping the header
data = np.genfromtxt('/home/labex/project/my_data.csv', delimiter=',', skip_header=1)
## Print the resulting array
print(data)
Save the file and run it again in the terminal:
python main.py
The output will now look much better:
[[ 1. 22.5 45. ]
[ 2. 23.1 48. ]
[ 3. nan 46. ]
[ 4. 23.5 52. ]]
As you can see, the data is now structured into a 2D array (two-dimensional array). Think of it like a table or spreadsheet with rows and columns - our array has 4 rows (one for each sensor reading) and 3 columns (Sensor ID, Temperature, Humidity). The numbers are correctly parsed as floats (floating-point numbers, which can represent decimal values like 22.5). However, notice the nan in the third row. This is because our source file contains the text NA to represent a missing temperature reading, and genfromtxt doesn't recognize it as a number. We'll address this in the next step.
Handling Missing Values
Real-world datasets are often incomplete. genfromtxt provides a clean way to handle this using the missing_values and filling_values arguments.
missing_values: A string or a list of strings that should be interpreted as missing data.filling_values: A value to substitute for any missing entries.
In our data, the missing value is represented by NA. Let's tell genfromtxt to recognize NA as a missing value and replace it with -99 for easy identification.
Update your main.py file as follows:
import numpy as np ## Import NumPy library
## Handle missing values
data = np.genfromtxt('/home/labex/project/my_data.csv', delimiter=',', skip_header=1,
missing_values='NA', filling_values=-99)
## Print the resulting array
print(data)
Save the file and execute it:
python main.py
The output now shows a complete numerical array, with the missing value replaced:
[[ 1. 22.5 45. ]
[ 2. 23.1 48. ]
[ 3. -99. 46. ]
[ 4. 23.5 52. ]]
Now our data is clean and fully numeric, ready for calculations.
Selecting Columns and Setting Data Types
Sometimes, you only need a subset of the data. The usecols argument lets you specify which columns to import. It takes a tuple (an immutable sequence of values, like (1, 2)) of column indices (starting from 0). For example, usecols=(1, 2) means "import only columns 1 and 2".
Additionally, you can enforce a specific data type for all imported data using the dtype argument. In programming, data types determine how values are stored and what operations can be performed on them. For example, dtype=int will convert all values to integers (whole numbers), dtype=float ensures they remain as floating-point numbers (decimals), and dtype=str treats them as text. Note that dtype=int will truncate any decimal parts (22.5 becomes 22).
Let's modify our script to import only the Temperature (column 1) and Humidity (column 2) and ensure they are treated as floating-point numbers.
Update main.py one last time:
import numpy as np ## Import NumPy library
## Select specific columns and set data type
data = np.genfromtxt('/home/labex/project/my_data.csv', delimiter=',', skip_header=1,
missing_values='NA', filling_values=0,
usecols=(1, 2), dtype=float)
## Print the resulting array
print(data)
Note: We changed
filling_valuesto0for this example.
Save the file and run it from the terminal:
python main.py
The final output will be a 2D array containing only the temperature and humidity data:
[[22.5 45. ]
[23.1 48. ]
[ 0. 46. ]
[23.5 52. ]]
You have successfully imported and cleaned a dataset by selecting only the relevant columns and handling all data inconsistencies along the way.
Summary
In this lab, you have learned how to effectively use numpy.genfromtxt to import data from a text file into a NumPy array. You practiced using several key arguments to handle real-world data challenges:
delimiter: To specify how columns are separated.skip_header: To ignore header lines in the data file.missing_values: To identify custom strings that represent missing data.filling_values: To replace missing data with a specific value.usecols: To import only a specific subset of columns.dtype: To control the data type of the resulting array.
Mastering genfromtxt is a fundamental skill for any data scientist or engineer working with Python and NumPy.



