如何从 xlsx 文件中读取并将特定列值存储到 python 中的数组中?

How to read from xlsx file and storing specific column values into an array in python?

大家好我正在尝试弄清楚如何从 xlsx 文件中读取数据并将其存储到数组中。在 python27 上,我可以下载最好的库是什么?

我想弄清楚如何读取特定列,将列中的值获取到特定单元格,然后将其存储到数组中?

示例:对于我的线性回归分析,我有一个 Y 变量和一个多变量 X 方程。

将从特定列读取 Y 值

例如:

y= [1,2,3,4,3,4,5,4,5,5,4,5,4,5,4,5,6,5,4,5,4,3,4] #<-specific column

其中多变量 X 值将从多列读取数据值

例如:

 x = [
    [4,2,3,4,5,4,5,6,7,4,8,9,8,8,6,6,5,5,5,5,5,5,5],      #<-specific column
     [4,1,2,3,4,5,6,7,5,8,7,8,7,8,7,8,7,7,7,7,7,6,5],      #<-specific column
     [4,1,2,5,6,7,8,9,7,8,7,8,7,7,7,7,7,7,6,6,4,4,4]       #<-specific column
     ]

谢谢

您可以使用openpyxl and should find all needed information in their documenation

另一个选项是Win32Com。我已经将它与 python 2.7 一起使用,以处理 Excel 和 Word 文件。

Pandas 对这种类型的操作非常有用。

import pandas as pd
xl_workbook = pd.ExcelFile("my_data.xlsx")  # Load the excel workbook
df = xl_workbook.parse("Sheet 1")  # Parse the sheet into a dataframe
x1_list = df['x1'].tolist()  # Cast the desired column into a python list

具体来说,当您通过列索引调用 df 时,它 returns 是一个 Numpy 系列,而 .tolist() 将其强制转换为 python 列表。