本文介绍了将Excel文件加载到numpy 2D数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经看过 numpy.genfromtxt

code>从numpy文档中自动加载功能,但它不直接加载excel文件。

  array = np.genfromtxt Stats.xlsx)
ValueError:检测到一些错误!
行#3(得到2列而不是1)
行#5(得到5列而不是1)
......

现在我正在使用 openpyxl.reader.excel 来阅读excel文件,然后附加到numpy 2D数组。这似乎效率不高。
理想情况下,我想要将excel文件直接加载到numpy 2D数组。

解决方案

老实说,使用是比直接使用 numpy 更好的选择。



虽然 pandas 在某种意义上只是一个包装器,但它非常非常好地处理异构数据。 (除了一些其他的东西...对于电子表格样数据,它是python世界的黄金标准。)



如果你决定去该路由,只需使用。


Is there an easier way to load an excel file directly into a Numpy array?

I have looked at the numpy.genfromtxt autoloading function from numpy documentation but it doesn't load excel files directly.

array = np.genfromtxt("Stats.xlsx")
ValueError: Some errors were detected !
Line #3 (got 2 columns instead of 1)
Line #5 (got 5 columns instead of 1)
......

Right now I am using using openpyxl.reader.excel to read the excel file and then append to numpy 2D arrays. This seems to be inefficient.Ideally I would like to have to excel file directly loaded to numpy 2D array.

解决方案

Honestly, if you're working with heterogeneous data (as spreadsheets are likely to contain) using a pandas.DataFrame is a better choice than using numpy directly.

While pandas is in some sense just a wrapper around numpy, it handles heterogeneous data very very nicely. (As well as a ton of other things... For "spreadsheet-like" data, it's the gold standard in the python world.)

If you decide to go that route, just use pandas.read_excel.

这篇关于将Excel文件加载到numpy 2D数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:52