How to read CSV data into a record array in NumPy?

In this tutorial, we will learn how to read CSV data into a record array in NumPy? By Pranit Sharma Last updated : May 23, 2023

CSV files or Comma Separated Values files are plain text files but the format of CSV files is tabular in nature. As the name suggests, in a CSV file, each specific value inside the CSV file is generally separated by a comma. The first line identifies the name of a data column. The further subsequent lines identify the values in rows.

Col_1_value, col_2_value , col_3_value
Row1_value1 , row_1_value2 , row_1_value3
Row1_value1 , row_1_value2 , row_1_value3

Here, the separator character (,) is called a delimiter. There are some more popular delimiters. E.g.: tab(\t), colon (:), semi-colon (;) etc.

Reading CSV data into a record array in NumPy

To read CSV data into a record array in NumPy, you can use pandas.read_csv() by passing the file name in it. The method reads a comma-separated values (csv) file into DataFrame and then convert it into an array.

Let us understand with the help of an example,

Python program to read CSV data into a record array in NumPy

# Import numpy
import numpy as np

# Import pandas
import pandas as pd

# Load csv file
data = pd.read_csv('appended_csv.csv')

# Display Original data
print("Original Data:\n",data,"\n")

# converting data into array
res = data.values

# Display Result
print("Result:\n",res,"\n")

Output

The output of the above program is:

Example: How to read CSV data into a record array in NumPy?frame

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.