Numpy loadtxt() Explained with Examples

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 10, 2024
  • Reading time:16 mins read

Python NumPy loadtxt() function is used to load the data from a text file and store them in a ndarray. The purpose of loadtxt() function is to be a fast reader for simple text files. Each row in the text file must have the same number of values.

In this article, I will explain Python numpy.loadtxt() function syntax and how we can use its parameters while reading the data from the text files with examples.

1. Quick Examples of NumPy loadtxt() Function

If you are in a hurry, below are some quick examples of how to use loadtxt() function.


# Quick examples of numpy loadtxt() function

# Example 1: numpy read txt file 
# Using numpy.loadtxt() function
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
arr2 = np.loadtxt(arr) 

# Example 2: Use numpy.loadtxt() function 
# To set dtype parameter
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
arr2 = np.loadtxt(arr,dtype="int")

# Example 3: Use set delimiter parameter 
# Use numpy.loadtxt() function
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
arr2 = np.loadtxt(arr,dtype="int",delimiter=",")

# Example 4: Use Set usecols Parameter 
# Use numpy.loadtxt() function
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
arr2 = np.loadtxt(arr,dtype="int",usecols =(1,2))

# Example 5: Use set unpack parameter 
# Use numpy.loadtxt() function
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
(x,y,z) = np.loadtxt(arr,dtype="int",unpack=True)

# Example 6: Set delimiter, usecols, unpack Parameter
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
x,y,z = np.loadtxt(arr,delimiter =', ', usecols =(0,1,2), unpack = True)

2. Syntax of NumPy loadtxt()

Following is the syntax of loadtxt() Function


# Syntax of NumPy loadtxt() 
numpy.loadtxt(fname, dtype=’float’, comments=’#’, delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0 encoding='bytes', max_rows=None)

2.2 Parameters of loadtxt()

Following are the parameters of the loadtxt().

  • fname – File, filename, or generator to read. If the filename extension,Path of txt file to be imported.
  • dtype – Data type of the resulting array.
  • comments – Characters or a list of characters are used to indicate the start of a comment.
  • delimiter – The string is used to separate values. By default, this is any whitespace. The delimiter to use for parsing the content of txt a file.
  • converters – A dictionary mapping column number to a function that will parse the column string into the desired value. Default: None.
  • skiprows – Skip the first skip rows lines, including comments; default: 0.
  • usecols – The column indices to be read.
  • unpack – If True, the returned array is transposed, so that arguments may be unpacked using x, y, z = loadtxt(…). [unpack=True]
  • ndmin – The minimum number of dimensions in the returned array.
  • encoding – Encoding is used to decode the input file.
  • max_rows – The maximum number of rows to read after skiprows lines.

2.3 Return value of loadtxt()

It returns ndarray which is loaded with the data from the text file.

Note: This function aims to be a fast reader for simply formatted files. The genfromtxt() function provides more sophisticated handling of, e.g., lines with missing values.

3. Read Data from text File Use NumPy loadtxt()

numpy.loadtxt() is used to return the n-dimensional NumPy array by reading the data from the text file, with an aim to be a fast reader for simple text files. 

This function numpy.loadtxt() can be used with both absolute and relative paths. It loads the data from the text file into the NumPy array. For that we need to import two Python modules one is NumPy, and the other is StringIO from the io module. Anything inside the StringIO is a File Object. The following example reads a text from a String object.

Note: The number of columns before the newline operator(\n) and after should be the same.


import numpy as np

# StringIO behaves like a file object
from io import StringIO    
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36") 

# Read data from text file 
arr2 = np.loadtxt(arr) 
print("After reading data from the text file:\n",arr2)

Yields below output.

numpy loadtxt

4. Read Text Data with dtype

By default, it reads the data as a float. you can change this by using the dtype parameter. The following example loads all the elements into an array from the text as integers.


# Read data use numpy.loadtxt() with dtype parameter
arr2 = np.loadtxt(arr,dtype="int")
print("After reading data with dtype:\n",arr2)

Yields below output.

numpy loadtxt

5. Use loadtxt() along with delimiter

By default, it uses whitespace as a delimiter. You can manually set the delimiter using the delimiter parameter. The following example reads the text separated by a comma delimiter.


# Read data use numpy.loadtxt() with delimiter parameter
arr = StringIO("5, 8, 11 \n14, 19, 21 \n 24, 32, 36") 
arr2 = np.loadtxt(arr,dtype="int",delimiter=",")
print("After reading data along with delimiter:\n",arr2)

# Output:
# After reading data along with delimiter:
#  [[ 5  8 11]
#  [14 19 21]
#  [24 32 36]]

6. Read Text Data with usecols

Use usecols parameter to specify which columns to be read from the txt file. The following example reads only the second and third columns from the txt file into the array.


# Read data use numpy.loadtxt() with usecols parameter
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36") 
arr2 = np.loadtxt(arr,dtype="int",usecols =(1,2))
print("After reading data with usecols:\n",arr2)

# Output:
# After reading data with usecols:
#  [[ 8 11]
#  [19 21]
#  [32 36]]

7. Using Numpy loadtxt() with unpack Param

You can also use transpose the array and unpacks the rows of the transposed array into specified variables.


# Read data use numpy.loadtxt() with unpack parameter
arr = StringIO("5 8 11 \n14 19 21 \n 24 32 36")
(x,y,z) = np.loadtxt(arr,dtype="int",unpack=True)
print(x)
print(y)
print(z)

# Output:
# [ 5 14 24]
# [ 8 19 32]
# [11 21 36]

Frequently Asked Questions

What is numpy.loadtxt() used for?


numpy.loadtxt() is a function in the NumPy library of Python that is used for loading data from a text file. It’s particularly useful for reading numerical data files into NumPy arrays. This function allows you to read data from a plain text file where the data is organized in rows and columns, such as CSV (Comma Separated Values) files or tab-delimited files.

What file formats can numpy.loadtxt() handle?

numpy.loadtxt() is primarily designed to handle plain text files where data is organized in rows and columns. It is often used for comma-separated values (CSV) files, tab-delimited files, and other similar formats.

Can numpy.loadtxt() handle missing values?

The numpy.loadtxt() function cannot handle missing values out of the box. If your text file contains missing or incomplete data, numpy.loadtxt() will raise a ValueError because it expects a uniform shape for the input data.

How to handle non-numeric data in a file with numpy.loadtxt()?

The numpy.loadtxt() is designed for numerical data. If your file contains non-numeric data, you might encounter errors. In such cases, consider using other functions or methods to handle non-numeric data before using numpy.loadtxt() numeric data.

How to handle files with headers using numpy.loadtxt()

Handling files with headers using numpy.loadtxt() is straightforward. If your file contains headers (the first row with column names), you can skip that row using the skiprows parameter. For example, if the header is in the first row, you can set skiprows=1 to skip the header row.

Conclusion

In this article, I have explained the Python NumPy loadtxt() function to load the data from the text file. Also covered using dtype, delimiter, usecols, and unpack Parameters with examples.

Happy Learning!!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.