如何将 CSV 数据读入 NumPy 中的记录数组? [英] How do I read CSV data into a record array in NumPy?

查看:29
本文介绍了如何将 CSV 数据读入 NumPy 中的记录数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种直接的方法可以将 CSV 文件的内容导入到记录数组中,就像 R 的 read.table(), read.delim()read.csv() 族将数据导入到 R 的数据框?

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?

或者是使用csv.reader()<的最佳方式/a> 然后应用类似 numpy.core.records.fromrecords()?

推荐答案

您可以使用 Numpy 的 genfromtxt() 方法来做到这一点,方法是将 delimiter kwarg 设置为逗号.

You can use Numpy's genfromtxt() method to do so, by setting the delimiter kwarg to a comma.

from numpy import genfromtxt
my_data = genfromtxt('my_file.csv', delimiter=',')

有关该函数的更多信息可以在其各自的文档中找到.

More information on the function can be found at its respective documentation.

这篇关于如何将 CSV 数据读入 NumPy 中的记录数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆