简体   繁体   English

python中按单元格值的列索引号

[英]Column index number by cell value in python

I am new to python and I am having a hard time with this issue and i need your help. 我是python的新手,这个问题很难解决,需要您的帮助。

Q1 Q2 Q3 Q4 Q5
25 9  57 23 7
61 41 29 5  57
54 34 58 10 7
13 13 63 26 45
31 71 40 40 40
24 38 63 63 47
31 50 43 2  61
68 33 13 9  63
28 1  30 39 71

I have an excel report with the data above. 我有上面数据的excel报告。 I'd like to write a code that looks through all columns in the 1st row and output the index number of the column with S in the column value (ie, 3). 我想编写一个代码,该代码可以浏览第一行中的所有列,并输出该列的索引号(在列值(即3)中带有S)。 I want to use the index number to extract data for that column. 我想使用索引号为该列提取数据。 I do not want to use row and cell reference as the excel file gets updated regularly, thus d column will always move. 我不想使用行和单元格引用,因为excel文件会定期更新,因此d列将始终移动。

def find_idx():
    wb = xlrd.open_workbook(filename='data.xlsx')  # open report
    report_sheet1 = wb.sheet_by_name('Sheet 1')

    for j in range(report_sheet1.ncols): 
        j=report_sheet1.cell_value(0, j)
        if 'YTD' in j:
            break
        return j.index('Q4')
find_idx()

the i get "substring not found" erro 我得到“找不到子字符串”错误

What i want is to return the column index number (ie, 3), so that i can call it easily in another code. 我想要的是返回列索引号(即3),以便我可以轻松地在另一个代码中调用它。 How can i fix this? 我怎样才能解决这个问题?

Hass! 哈斯!

As far as I understood, you want to get the index of a column of an excel file whose name contains a given substring such as Y . 据我了解,您想获取一个Excel文件的列的索引,该文件的名称包含给定的子字符串,例如Y Is that right? 那正确吗?

If so, here's a working snippet that does not requires pandas: 如果是这样,这是不需要熊猫的有效代码段:

import xlrd


def find_idx(excel_filename, sheet_name, col_name_lookup):
    """
    Returns the column index of the first column that
    its name contains the string col_name_lookup. If
    the col_name_lookup is not found, it returns -1.
    """
    wb = xlrd.open_workbook(filename=excel_filename)
    report_sheet1 = wb.sheet_by_name(sheet_name)

    for col_ix in range(report_sheet1.ncols):
        col_name = report_sheet1.cell_value(0, col_ix)

        if col_name_lookup in col_name:
            return col_ix

    return -1


if __name__ == "__main__":
    excel_filename = "./data.xlsx"
    sheet_name = "Sheet 1"
    col_name_lookup = "S"

    print(find_idx(excel_filename, sheet_name, col_name_lookup))

I tried to give more semantic names to your variables (I transformed your variable j into two other variables: col_ix (actual column index of the loop) and also the variable col_name which really stands for the column name. 我试图给变量赋予更多的语义名称(我将变量j转换为另外两个变量: col_ix (循环的实际列索引)以及变量col_name ,它实际上代表列名。

This code assumes that the first line of your excel file contains the column names, and if your desired substring to be looked in each of these names is not found, it returns -1. 此代码假定excel文件的第一行包含列名称,并且如果未找到要在每个这些名称中查找的所需子字符串,则它将返回-1。

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

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