![]() |
|
[Tut] How to Convert a CSV to NumPy Array in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert a CSV to NumPy Array in Python? (/thread-99684.html) |
[Tut] How to Convert a CSV to NumPy Array in Python? - xSicKxBot - 07-11-2022 How to Convert a CSV to NumPy Array in Python? 5/5 – (1 vote) Problem Formulation Given a CSV file (e.g., stored in the file with name Code: 'my_file.csv'Code: INPUT: file 'my_file.csv'
9,8,7
6,5,4
3,2,1![]() Challenge: How to convert it to a NumPy Array? Code: OUTPUT: 2D NumPy Array
[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![]() np.loadtxt() You can convert a CSV file to a NumPy array simply by calling Code: np.loadtxt()Code: filenameCode: delimiterCode: np.loadtxt('my_file.csv', delimiter=',')Code: 'my_file.csv'Code: ','Here’s an example: Code: import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)Output: Code: [[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![]() np.loadtxt() + header You can convert a CSV file with first-line header to a NumPy array by calling Code: np.loadtxt()Code: filenameCode: skiprows=1Code: delimiterCode: np.loadtxt('my_file.csv', skiprows=1, delimiter=',')Code: 'my_file.csv'Code: ',' [i]Figure: Skip the first header line in the CSV using the Code: skiprowsCode: np.loadtxt()Here’s an example: Code: import numpy as np array = np.loadtxt('my_file.csv', skiprows=1, delimiter=',')
print(array)Output: Code: [[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![]() CSV Reader To convert a CSV file Code: 'my_file.csv'Code: csv.reader(file_obj)Code: list()Code: np.array(list)Here’s an example: Code: import numpy as np
import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(reader) print(lst)The output is the list of lists: Code: [['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]Now, if you need to convert it to a NumPy array, you can simply use the Code: np.array()Code: array = np.array(lst)
print(array)Output: Code: [['9' '8' '7'] ['6' '5' '4'] ['3' '2' '1']] Related Tutorial: How to Convert CSV to List of Lists in PythonMethod 4: np.genfromtxt() ![]() np.genfromtxt() You can convert a CSV file to a NumPy array simply by calling Code: np.genfromtxt()Code: filenameCode: delimiterCode: np.genfromtxt('my_file.csv', delimiter=',')Code: 'my_file.csv'Code: ','Here’s an example: Code: import numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)Output: Code: [[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]![]() read_csv() and df.to_numpy() A quick and efficient way to read a CSV to a NumPy array is to combine Pandas’ Code: pd.read_csv()Code: df.to_numpy()Here’s an example: Code: import pandas as pd df = pd.read_csv('my_file.csv', header=None)
array = df.to_numpy() print(array)Output: Code: [[9 8 7] [6 5 4] [3 2 1]] Related Tutorial: 17 Ways to Read a CSV File to a Pandas DataFrameSummary We have seen five ways to convert a CSV file to a 2D NumPy array:
Our preferred way is Code: np.loadtxt() |