是否有一种直接的方法将CSV文件的内容导入到记录数组中,就像R的read.table(), read.delim()和read.csv()将数据导入到R数据框架中一样?
或者我应该使用csv.reader(),然后应用numpy.core.records.fromrecords()?
是否有一种直接的方法将CSV文件的内容导入到记录数组中,就像R的read.table(), read.delim()和read.csv()将数据导入到R数据框架中一样?
或者我应该使用csv.reader(),然后应用numpy.core.records.fromrecords()?
当前回答
还可以尝试recfromcsv(),它可以猜测数据类型并返回正确格式化的记录数组。
其他回答
我试了一下:
import pandas as p
import numpy as n
closingValue = p.read_csv("<FILENAME>", usecols=[4], dtype=float)
print(closingValue)
使用numpy.loadtxt
一个非常简单的方法。但它要求所有元素都是float (int等)
import numpy as np
data = np.loadtxt('c:\\1.csv',delimiter=',',skiprows=0)
这件作品很有魅力……
import csv
with open("data.csv", 'r') as f:
data = list(csv.reader(f, delimiter=";"))
import numpy as np
data = np.array(data, dtype=np.float)
这是最简单的方法:
import csv
with open('testfile.csv', newline='') as csvfile:
data = list(csv.reader(csvfile))
现在数据中的每个条目都是一个记录,表示为一个数组。你有一个二维数组。这节省了我很多时间。
这是一个非常简单的任务,最好的方法如下
import pandas as pd
import numpy as np
df = pd.read_csv(r'C:\Users\Ron\Desktop\Clients.csv') #read the file (put 'r' before the path string to address any special characters in the file such as \). Don't forget to put the file name at the end of the path + ".csv"
print(df)`
y = np.array(df)