To read a CSV file using NumPy in Python, we can use the genfromtxt() function of NumPy. Here's an example code:
main.py225 chars11 lines
In this code, we first import the NumPy library. Then, we use the genfromtxt() function to read the CSV file called data.csv and store its contents in a 2D NumPy array called data. The delimiter=',' argument specifies that the CSV file is comma-separated.
We also print out the shape of the 2D NumPy array and its contents.
Note: If there are missing values in the CSV file, you can use the filling_values parameter to replace them with a specified value.
gistlibby LogSnag