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

时间:2015-09-09 13:02:55

标签: python arrays excel regression

Hello社区我试图找出如何从xlsx文件中读取数据并将其存储到数组中。我可以在python27上下载哪个最好的库?

我试图找出如何读取特定列,从列中获取值到某个单元格,然后将其存储到数组中?

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

将从特定列

中读取Y值

前:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menuToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:background="@color/white"
android:contentInsetLeft="10dp"
android:contentInsetRight="10dp"
android:contentInsetStart="10dp"
android:minHeight="?attr/actionBarSize"
android:padding="0dp"
app:contentInsetLeft="10dp"
app:contentInsetRight="10dp"
app:contentInsetStart="10dp"></android.support.v7.widget.Toolbar>

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

例如:

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

由于

3 个答案:

答案 0 :(得分:2)

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时,它返回一个Numpy系列,而.tolist()将它强制转换为python列表。

答案 1 :(得分:0)

您可以使用openpyxl并在documenation

中找到所有必需的信息

答案 2 :(得分:0)

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