在Pandas中使用电子表格名称作为变量

时间:2016-11-07 20:47:44

标签: python excel function pandas spreadsheet

我在名为RateMatrix1的Excel文件中存储了不同的模型和initialValues。我的模型是WIN,WSW,WPA,WFR ......我的initialValues是WI,WS,WP,WF ......而且Excel上的工作表都是这样命名的。

现在,我想编写一个函数,它使用模型的名称和initialValues作为下面的“sheetnames”。我想知道是否有办法在python中这样做。

import pandas as pd
import numpy as np

def MLA(model, initialValues)
    RMatrix=(pd.read_excel("C:\Anaconda3\RateMatrix1.xlsx", sheetname="model", skiprows=0)).as_matrix() #read the matrix values from excel spreadsheet, and converts the values to a matrix
    initialAmount = (pd.read_excel("C:\Anaconda3\RateMatrix1.xlsx", sheetname="initialValues", skiprows=0)).as_matrix() #read the column matrix (initial values) from excel spreadsheet, and converts the values to a matrix
    return np.dot(RMatrix,initialAmount)

print(MLA(WIN,WI))

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

没关系......我找到了解决方案。

对于其他想要做同样事情的人,这是我的代码:

import pandas as pd
import numpy as np

def MLA(model, initialValues)
    RMatrix=(pd.read_excel("C:\Anaconda3\RateMatrix1.xlsx", sheetname=model, skiprows=0)).as_matrix() #read the matrix values from excel spreadsheet, and converts the values to a matrix
    initialAmount = (pd.read_excel("C:\Anaconda3\RateMatrix1.xlsx", sheetname=initialValues, skiprows=0)).as_matrix() #read the column matrix (initial values) from excel spreadsheet, and converts the values to a matrix
return np.dot(RMatrix,initialAmount)

print(MLA("WIN","WI"))