In Chapter 2, NumPy and pandas, we looked at the NumPy library in detail and explored lots of functionality. NumPy also has functions to read and write CSV files and get output in a NumPy array. The genfromtxt() function will help us to read the data and the savetxt() function will help us to write the data into a file. The genfromtxt() function is slow compared to other functions due to its two-stage operation. In the first stage, it reads the data in a string type, and in the second stage, it converts the string type into suitable data types. genfromtxt() has the following parameters:
- fname: String; filename or path of the file.
- delimiter: String; optional, separate string value. By default, it takes consecutive white spaces.
- skip_header: Integer; optional, number of lines you want to skip from the start of the file.
Let's see an example of reading and writing CSV files:
# import genfromtxt function
from numpy import genfromtxt
# Read comma...