如何使用python将excel数据读取到数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28774960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get read excel data into an array with python
提问by griffinc
In the lab that I work in, we process a lot of data produced by a 96 well plate reader. I'm trying to write a script that will perform a few calculations and output a bar graph using matplotlib.
在我工作的实验室中,我们处理了由 96 孔读板机产生的大量数据。我正在尝试编写一个脚本,该脚本将执行一些计算并使用 matplotlib 输出条形图。
The problem is that the plate reader outputs data into a .xlsx file. I understand that some modules like pandas have a read_excel function, can you explain how I should go about reading the excel file and putting it into a dataframe?
问题是读板器将数据输出到 .xlsx 文件中。我知道像 Pandas 这样的一些模块有一个 read_excel 函数,你能解释一下我应该如何读取 excel 文件并将其放入数据帧吗?
Thanks
谢谢
Data sample of a 24 well plate (for simplicity):
24 孔板的数据样本(为简单起见):
0.0868 0.0910 0.0912 0.0929 0.1082 0.1350
0.0466 0.0499 0.0367 0.0445 0.0480 0.0615
0.6998 0.8476 0.9605 0.0429 1.1092 0.0644
0.0970 0.0931 0.1090 0.1002 0.1265 0.1455
采纳答案by griffinc
This task is super easy in Pandas these days.
如今,这项任务在 Pandas 中非常容易。
import pandas as pd
import pandas as pd
df = pd.read_excel('file_name_here.xlsx', sheet_name='Sheet1')
df = pd.read_excel('file_name_here.xlsx', sheet_name='Sheet1')
or
或者
df = pd.read_csv('file_name_here.csv')
df = pd.read_csv('file_name_here.csv')
This returns a pandas.DataFrame
object which is very powerful for performing operations by column, row, over an entire df, or over individual items with iterrows. Not to mention slicing in different ways.
这将返回一个pandas.DataFrame
非常强大的对象,用于按列、行、整个 df 或具有 iterrows 的单个项目执行操作。更不用说以不同的方式切片了。
回答by Franco Solleza
I'm not exactly sure what you mean when you say array, but if you mean into a matrix, might you be looking for:
当你说数组时,我不确定你的意思,但如果你的意思是矩阵,你可能正在寻找:
import pandas as pd
df = pd.read_excel([path here])
df.as_matrix()
This returns a numpy.ndarray type.
这将返回一个 numpy.ndarray 类型。
回答by Alexey Milogradov
There is awesome xlrd
package with quick start example here.
You can just google it to find code snippets. I have never used panda
's read_excel
function, but xlrd
covers all my needs, and can offer even more, I believe.
有真棒xlrd
包快速入门示例在这里。你可以用谷歌搜索它来查找代码片段。我从未使用过panda
的read_excel
功能,但xlrd
我相信它涵盖了我所有的需求,并且可以提供更多。
回答by chfw
You could also try it with my wrapper library, which uses xlrd as well:
您也可以尝试使用我的包装库,它也使用 xlrd:
import pyexcel as pe # pip install pyexcel
import pyexcel.ext.xls # pip install pyexcel-xls
your_matrix = pe.get_array(file_name=path_here) # done