You can use the following basic syntax to read a CSV file into a record array in NumPy:
from numpy import genfromtxt my_data = genfromtxt('data.csv', delimiter=',', dtype=None)
The following step-by-step example shows how to use this syntax in practice.
Step 1: View the CSV File
Suppose we have the following CSV file called data.csv that we’d like to read into NumPy:
Step 2: Read in CSV File
The following code shows how to read in this CSV file into a Numpy array:
from numpy import genfromtxt #import CSV file my_data = genfromtxt('data.csv', delimiter=',', dtype=None)
Note the following:
- delimiter: This specifies the delimiter that separates the data values in the CSV file.
- dtype: This specifies the data type for the NumPy array. By using None, we allow multiple data types to be imported at once within the array.
Example 3: View the NumPy Array
Once we’ve imported the CSV file, we can view it:
#view imported CSV file my_data array([[1, 2, 2, 2, 3, 4], [5, 5, 6, 8, 9, 9]])
We can see that the data in the NumPy array matches the data shown in the CSV file.
Note: You can find the complete online documentation for the genfromtxt() function here.
The following tutorials explain how to perform other common functions with CSV files in pandas:
How to Read CSV Files with Pandas
How to Export Pandas DataFrame to CSV File
Pandas: How to Append Data to Existing CSV File