Explore Courses Blog Tutorials Interview Questions
0 votes
in Python by (47.6k points)

I wonder if there is a direct way to import the contents of a CSV file into a record array, much in the way that R's read.table(), read.delim(), and read.csv() family imports data to R's data frame?

Or is the best way to use csv.reader() and then apply something like numpy.core.records.fromrecords()?

1 Answer

0 votes
by (106k points)

To read CSV data into a record array in NumPy you can use NumPy modules genfromtxt() function, In this function’s argument, you need to set the delimiter to a comma.

from numpy import genfromtxt

my_data = genfromtxt('my_file.csv', delimiter=',')

You can also use the pandas read_csv function to read CSV data into a record array in NumPy.

import pandas as pd

df=pd.read_csv('myfile.csv', sep=',',header=None)

df.values array([[ 1. , 2. , 3. ], [ 4. , 5.5, 6. ]])

if you wish to know what is python visit this python tutorial and python interview questions.

Related questions

Browse Categories