简体   繁体   中英

How to iterate over excel columns

(This might be an easy question, but I am fairly new to python). I have a function with multiple arguments (18 arguments). I also have an excel file which contains the input data that I want to pass to the function. I assume that the first step is to convert the excel file into a dataframe. 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):

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?

check this out: A Guide to Excel Spreadsheets in Python With openpyxl

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

column_names=list(df.columns)

column_names will contain a list of columns that you have in your excel file. 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.

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 is a COM wrapper which means it supports a two-way communication between Excel and 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.

You would need to install xlwings and follow the xlwings instructions for setting up the Excel workbook (macro enabled, trust and add-in). 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.

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.

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. 在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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