🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.loadtxt()

AI & DATA SCIENCE // np-loadtxt

np.loadtxt() reads numeric data from a plain text file into an array, the counterpart to np.savetxt(), but requires the data to be complete and well-formed.

Syntax

np.loadtxt(file, delimiter=None, skiprows=0)

Deep Dive Course

loadtxt() expects every row to have the same number of columns and every value to be parseable as a number — it raises an error immediately if it encounters a missing value, an inconsistent row length, or non-numeric text it can't convert. The delimiter parameter specifies how columns are separated, whitespace by default, or a comma for typical CSV files, and skiprows lets you skip a fixed number of header lines at the top of the file before the actual data begins.

1Understanding np.loadtxt()

loadtxt() expects every row to have the same number of columns and every value to be parseable as a number — it raises an error immediately if it encounters a missing value, an inconsistent row length, or non-numeric text it can't convert. The delimiter parameter specifies how columns are separated, whitespace by default, or a comma for typical CSV files, and skiprows lets you skip a fixed number of header lines at the top of the file before the actual data begins.

💡

loadtxt() has no built-in tolerance for missing or malformed values — it simply raises an error the moment it hits one; use np.genfromtxt() instead when your data might have missing values or minor inconsistencies that need to be handled gracefully.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")
loaded = np.loadtxt("data.csv", delimiter=",")
print(loaded)
localhost:3000

2Practical Example

Here is a real-world application of np.loadtxt() showing how it is used in production NumPy code.

editor.html
import numpy as np

with open("scores.csv", "w") as f:
    f.write("name,math,science\nAlice,90,85\nBob,78,92\n")

scores = np.loadtxt("scores.csv", delimiter=",", skiprows=1, usecols=(1, 2))
print(scores)
localhost:3000

3Best Practices

Follow these guidelines when working with np.loadtxt():

1. Use loadtxt() specifically for clean, well-formed numeric text data with no missing values, where its speed and simplicity are an advantage

2. Use skiprows to skip header lines instead of manually stripping them from the file first

3. Reach for np.genfromtxt() instead of loadtxt() as soon as missing values, inconsistent rows, or mixed data types are a realistic possibility

⚠️

Tip: loadtxt() has no built-in tolerance for missing or malformed values — it simply raises an error the moment it hits one; use np.genfromtxt() instead when your data might have missing values or minor inconsistencies that need to be handled gracefully.

editor.html
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")
loaded = np.loadtxt("data.csv", delimiter=",")
print(loaded)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")
loaded = np.loadtxt("data.csv", delimiter=",")
print(loaded)
Example 02Advanced Example
import numpy as np

with open("scores.csv", "w") as f:
    f.write("name,math,science\nAlice,90,85\nBob,78,92\n")

scores = np.loadtxt("scores.csv", delimiter=",", skiprows=1, usecols=(1, 2))
print(scores)

Best Practices

  • Use loadtxt() specifically for clean, well-formed numeric text data with no missing values, where its speed and simplicity are an advantage
  • Use skiprows to skip header lines instead of manually stripping them from the file first
  • Reach for np.genfromtxt() instead of loadtxt() as soon as missing values, inconsistent rows, or mixed data types are a realistic possibility

Interview Question

What happens if np.loadtxt() encounters a row with a missing value, compared to how np.genfromtxt() would handle the exact same file?

Hint: Think about how tolerant each function is designed to be.

loadtxt() is designed for clean, fully well-formed numeric data — encountering a missing value, an inconsistent number of columns, or any text it can't parse as a number immediately raises an error and stops loading entirely, with no built-in way to tolerate or work around the problem. np.genfromtxt() is specifically designed to handle exactly this kind of messier, real-world data: it can fill in missing values with a placeholder like nan, using its missing_values and filling_values parameters, rather than failing outright, which is why it's the better choice whenever the input data isn't guaranteed to be perfectly clean.

Exercises

MediumPractice using np.loadtxt() in a real scenario.
View Solution
import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
np.savetxt("data.csv", arr, delimiter=",", fmt="%d")
loaded = np.loadtxt("data.csv", delimiter=",")
print(loaded)

Frequently Asked Questions

What happens if np.loadtxt() encounters a row with a missing value, compared to how np.genfromtxt() would handle the exact same file?

loadtxt() is designed for clean, fully well-formed numeric data — encountering a missing value, an inconsistent number of columns, or any text it can't parse as a number immediately raises an error and stops loading entirely, with no built-in way to tolerate or work around the problem. np.genfromtxt() is specifically designed to handle exactly this kind of messier, real-world data: it can fill in missing values with a placeholder like nan, using its missing_values and filling_values parameters, rather than failing outright, which is why it's the better choice whenever the input data isn't guaranteed to be perfectly clean.

Related Functions

np-savetxtnp-genfromtxtopen()