简体   繁体   English

如何迭代excel列

[英]How to iterate over excel columns

(This might be an easy question, but I am fairly new to python). (这可能是一个简单的问题,但我对 python 相当陌生)。 I have a function with multiple arguments (18 arguments).我有一个带有多个参数(18 个参数)的函数。 I also have an excel file which contains the input data that I want to pass to the function.我还有一个 excel 文件,其中包含我想传递给函数的输入数据。 I assume that the first step is to convert the excel file into a dataframe.我假设第一步是将 excel 文件转换为数据框。 However, my problem is that I don't know how I can pass the column values to the function.但是,我的问题是我不知道如何将列值传递给函数。

My function looks as follows (included 4 out of 18 arguments only for the example):我的函数如下所示(仅在示例中包含 18 个参数中的 4 个):

def calculation(calculation_date, redemption, spot_price, conversion_ratio):
    ... #calculations here
    return bond_data

My data is structured like this:我的数据结构如下:

在此处输入图片说明

How do I pass the values from from each column to the function?如何将每列中的值传递给函数? I structured my datafile so that the first column (column A) matches the first argument, the second column (column B) matches the second argument etc?我构造了我的数据文件,以便第一列(A 列)匹配第一个参数,第二列(B 列)匹配第二个参数等等?

check this out: A Guide to Excel Spreadsheets in Python With openpyxl看看这个:使用 openpyxl 在 Python 中使用 Excel 电子表格的指南

you can do something like this:你可以这样做:

from openpyxl import load_workbook

workbook = load_workbook(filename="sample.xlsx")
sheet = workbook.active
for row in sheet.iter_rows(min_row=2,
                       min_col=1,
                       max_col=4,
                       values_only=True):
    row_dic = {
               "parent": row[1],
               "title": row[2],
               "category": row[3]}
    excel_list.append(row_dic)

and you can use the list to call the calculation function.并且您可以使用列表来调用计算函数。

You can try this你可以试试这个

After reading excel file, if you have df as data frame object then读取 excel 文件后,如果您有df作为数据框对象,则

column_names=list(df.columns)

column_names will contain a list of columns that you have in your excel file. column_names 将包含您在 excel 文件中的列列表。 now you can iterate through it.现在您可以遍历它。 example例子

for column_name in column_names:
      #your code

Note: you can directly pass the column_names list object to the function.注意:您可以直接将column_names列表对象传递给函数。

Example:例子:

#function defination
def column_names(list_of_columns)

#calling function
calculation(column_names) 

let me know if this works for you or not.让我知道这是否适合您。

Thanks谢谢

This is possible with xlwings while remaining in Excel.这可以通过 xlwings 实现,同时保留在 Excel 中。

xlwings is a COM wrapper which means it supports a two-way communication between Excel and Python. xlwings 是一个 COM 包装器,这意味着它支持 Excel 和 Python 之间的双向通信。 With xlwings you can use some VBA to call out to a Python function, or define a "User Defined Function" and actually use your Python code as a function inside Excel.使用 xlwings,您可以使用一些 VBA 来调用 Python 函数,或定义“用户定义的函数”并实际将您的 Python 代码用作 Excel 中的函数。

You would need to install xlwings and follow the xlwings instructions for setting up the Excel workbook (macro enabled, trust and add-in).您需要安装 xlwings 并按照 xlwings 说明设置 Excel 工作簿(启用宏、信任和加载项)。 It's not terribly difficult.这不是特别困难。

In the below code I have chosen to use a User Defined Function which makes the my_calculation available as a function in Excel.在下面的代码中,我选择使用用户定义的函数,这使得 my_calculation 可作为 Excel 中的函数使用。

This is the code for a made-up calculation using the function signature you have defined.这是使用您定义的函数签名进行虚构计算的代码。 The "xlwings" bit is really just the decorators which have applied to the function. “xlwings”位实际上只是应用于函数的装饰器。

import xlwings as xw
import datetime

@xw.func
@xw.arg('calculation_date')
@xw.arg('redemption')
@xw.arg('spot_price')
@xw.arg('conversion_ratio')
def my_calculation(calculation_date, redemption, spot_price, conversion_ratio):
    bond_data = redemption / spot_price * conversion_ratio
    return bond_data

This is an image of what we see when using my_function in an Excel file using the xlwings add-in.这是我们使用 xlwings 加载项在 Excel 文件中使用 my_function 时看到的图像。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM