How to read CSV data into a record array in NumPy?
Last Updated :
03 Apr, 2025
In NumPy, a record array (or recarray) is a specialized array that allows you to access fields as attributes, providing a convenient way to handle structured data. It is essentially a structured array that is wrapped in an object-oriented interface, making it easier to work with data that has named columns, similar to a DataFrame in pandas.
Key Features of Record Arrays
- Named Fields: Each column in a record array can have a name, allowing you to access it using attribute-style access (e.g.,
array.fieldname
). - Mixed Data Types: Different fields can have different data types (e.g., integers, floats, strings).
- Convenient Access: You can access and modify the fields by their names, similar to how you work with attributes in objects.
Read CSV data into a record array in NumPy
- Using numpy.genfromtxt
- Using recfromcsv function
- Using pandas and converting to NumPy record array
Read CSV data into a record array in NumPy Using numpy.genfromtxt
In this approach example, we are using numpy.genfromtxt to read CSV data (geeks_data.csv) into a NumPy record array (data). The delimiter=',' specifies that fields are separated by commas, dtype=None allows NumPy to infer data types, names=True indicates that the first row contains column names, and encoding=None handles the default encoding.
Python
# Approach 1: Using numpy.genfromtxt
import numpy as np
csv_data = """
ID,Name,Salary
1,Gaurav,50000
2,Yuvraj,60000
3,Pranav,55000
"""
with open('geeks_data.csv', 'w') as f:
f.write(csv_data)
data = np.genfromtxt('geeks_data.csv', delimiter=',',
dtype=None, names=True, encoding=None)
print("Record array:")
print(data)
OutputRecord array:
[(1, 'Gaurav', 50000) (2, 'Yuvraj', 60000) (3, 'Pranav', 55000)]
Read CSV data into a record array in NumPy Using recfromcsv function
In this approach using np.recfromcsv, we directly read the CSV data (geeks_data.csv) into a NumPy record array (data). The function np.recfromcsv is specifically designed to handle CSV files and creates a structured array where each column can be accessed using its name.
Python
# Approach 2: Using recfromcsv function
import numpy as np
csv_data = """
ID,Name,Salary
1,Gaurav,50000
2,Yuvraj,60000
3,Pranav,55000
"""
with open('geeks_data.csv', 'w') as f:
f.write(csv_data)
data = np.recfromcsv('geeks_data.csv', encoding=None)
print("Record array:")
print(data)
OutputRecord array:
[(1, 'Gaurav', 50000) (2, 'Yuvraj', 60000) (3, 'Pranav', 55000)]
Read CSV data into a record array in NumPy Using pandas and converting to NumPy record array
In this approach example, we are using pandas to read the CSV data (geeks_data.csv) into a DataFrame (df). The read_csv function parses the CSV file, and then to_records() converts the DataFrame into a NumPy record array (data). Setting index=False ensures that the DataFrame index is not included in the resulting record array.
Python
# Approach 3: Using pandas and converting to NumPy record array
import numpy as np
import pandas as pd
csv_data = """
ID,Name,Salary
1,Gaurav,50000
2,Yuvraj,60000
3,Pranav,55000
"""
with open('geeks_data.csv', 'w') as f:
f.write(csv_data)
df = pd.read_csv('geeks_data.csv')
data = df.to_records(index=False)
print("Record array:")
print(data)
OutputRecord array:
[(1, 'Gaurav', 50000) (2, 'Yuvraj', 60000) (3, 'Pranav', 55000)]
Conclusion
In conclusion, parsing CSV data into NumPy record arrays using numpy.genfromtxt, np.recfromcsv, or pandas provides flexible options for handling structured data in Python. These approaches allow seamless integration of CSV data into NumPy arrays, enabling efficient data manipulation and analysis tasks within data science and numerical computing workflows.
Similar Reads
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
How to read numbers in CSV files in Python? Prerequisites: Reading and Writing data in CSV, Creating CSV files CSV is a Comma-Separated Values file, which allows plain-text data to be saved in a tabular format. These files are stored in our system with a .csv extension. CSV files differ from other spreadsheet file types (like Microsoft Excel
4 min read
How to Convert a Dataframe Column to Numpy Array NumPy and Pandas are two powerful libraries in the Python ecosystem for data manipulation and analysis. Converting a DataFrame column to a NumPy array is a common operation when you need to perform array-based operations on the data. In this section, we will explore various methods to achieve this t
2 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read