【问题标题】:How to extract a particular set of values from excel file using a numerical range in python?如何使用python中的数值范围从excel文件中提取一组特定的值?
【发布时间】:2021-04-11 05:03:20
【问题描述】:

我打算做什么:

我有一个包含电压和电流数据的 excel 文件,我想从特定表格中提取它,比如“IV_RAW”。这些值仅来自第 4 行,位于 D 和 E 列。 假设这些值如下所示:

V(voltage) I(Current)
47 1
46 2
45 3
0 4
-0.1 5
-10 5

现在,我只想取出以电压 (V) 为 45 且不应采用负电压的值。还需要取出相应的电流 (I) 值。这必须为多个 excel 文件完成。所以不能从特定的行号开始,而是电压值应该是标准。

我所知道的:

我只知道如何使用 openxyl 取出整组值:

loc = ("path")
wb = load_workbook("Data") #thefilename
ws = wb["IV_raw"] #theactiveworksheet 

#to extract the voltage and current data: 
for row in ws.iter_rows(min_row=1, max_col=3, max_row=2, values_only=True): 
      
        print(row)

我是一名中午编码员,并且是 python 新手。因此,如果你们能提供帮助,那将非常有帮助。如果有pandas 的简化版本,那就太好了。 提前谢谢你

【问题讨论】:

    标签: python arrays excel pandas distinct-values


    【解决方案1】:

    以下使用pandas,您一定要看看。使用 sheet_name 设置 sheet_name,header 是标题的行索引(从 0 开始,因此第 4 -> 3 行),usecols 使用 A1 表示法定义列。

    最后一行过滤数据框。如果我理解正确,那么您希望电压在 0 到 45 之间,这就是示例所做的,而 df 是您生成的 data_frame

    import pandas as pd
    file_loc = "path.xlsx"
    df = pd.read_excel(file_loc, 
                       sheet_name = 'IV_raw',
                       header = 3, 
                       usecols = "D:E")
    df = df[(df['V(voltage)'] > 0) & (df['V(voltage)'] < 45)]
    

    【讨论】:

      【解决方案2】:

      在您的示例的基础上,您可以使用以下示例来获得所需的内容

      from openpyxl import load_workbook
      
      wb = load_workbook(filepath,data_only=True) #load the file using its full path
      ws = wb["Sheet1"] #theactiveworksheet 
      
      #to extract the voltage and current data: 
      data = ws.iter_rows(min_col=4, max_col=5, min_row=2, max_row=ws.max_row, values_only=True)
      output = [row for row in data if row[0]>45]
      

      【讨论】:

        【解决方案3】:

        你可以试试这个,

        import openpyxl
        
        tWorkbook = openpyxl.load_workbook("YOUR_FILEPATH")
        tDataBase = tWorkbook.active
        
        voltageVal= "D4"
        currentVal= "E4"
        
        V = tDataBase[voltageVal].value
        I = tDataBase[currentVal].value
        

        【讨论】:

        • 答案是缺少 sheet_name 信息,以及非第一行起点。如果也缺乏解释
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 2016-06-06
        • 1970-01-01
        • 2015-06-22
        相关资源
        最近更新 更多