【问题标题】:Use Spreadsheet Names as Variables in Pandas在 Pandas 中使用电子表格名称作为变量
【发布时间】:2016-11-07 20:47:44
【问题描述】:

我在名为 RateMatrix1 的 Excel 文件中的不同工作表中存储了不同的模型和初始值。我的模型是 WIN、WSW、WPA、WFR……我的初始值是 WI、WS、WP、WF……,Excel 上的工作表就是这样命名的。

现在,我想编写一个函数,它使用模型名称和初始值作为下面的“工作表名称”。我想知道是否有办法在 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))

【问题讨论】:

    标签: python excel pandas function


    【解决方案1】:

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

    对于其他希望这样做的人,这是我的代码:

    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"))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多