简体   繁体   English

如何使用 python 中的数字范围从 excel 文件中提取一组特定的值?

[英]How to extract a particular set of values from excel file using a numerical range in python?

What I intend to do:我打算做什么:

I have an excel file with Voltage and Current data which I would like to extract from a specific sheet say 'IV_RAW'.我有一个 excel 文件,其中包含我想从特定表格中提取的电压和电流数据,例如“IV_RAW”。 The values are only from 4th row and are in columns D and E. Lets say the values look like this:这些值仅来自第 4 行,位于 D 和 E 列中。假设这些值如下所示:

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

Now, I just want to take out only the values starting with a voltage (V) of 45 and shouldnt take negative voltages .现在,我只想取出以电压 (V) 为 45 的值并且不应该取负电压 The corresponding current (I) values are also needed to be taken out.还需要取出相应的电流 (I) 值。 This has to be done for multiple excel files.这必须对多个 excel 文件进行。 So starting from a particular row number cannot be done instead voltage values should be the criterion.因此,不能从特定的行号开始,而是以电压值作为标准。

What I know:我知道的:

I know only how to take out the entire set of values using openxyl:我只知道如何使用 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)

I am a noon coder and new to python.我是一名中午编码员,是 python 的新手。 So it will be really helpful if you guys could help.因此,如果你们能提供帮助,那将非常有帮助。 If there is a simplified versions with pandas it will be really great.如果有pandas的简化版本,那就太好了。 Thank you in advance先感谢您

The following uses pandas which you should definitly take a look at.以下使用pandas您一定要看看。 with sheet_name you set the sheet_name, header is the row index of the header (starting at 0, so Row 4 -> 3), usecols defines the columns using A1 notation.使用sheet_name设置 sheet_name, header是 header 的行索引(从 0 开始,因此行 4 -> 3), usecols使用 A1 表示法定义列。

The last line filters the dataframe.最后一行过滤 dataframe。 If I understand correctly, then you want Voltage between 0 and 45, thats what the example does and df is your resulting data_frame如果我理解正确,那么您希望电压在 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)]

Building on from your example, you can use the following example to get what you need在您的示例的基础上,您可以使用以下示例来获取您需要的内容

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]

you can try this,你可以试试这个

import openpyxl

tWorkbook = openpyxl.load_workbook("YOUR_FILEPATH")
tDataBase = tWorkbook.active

voltageVal= "D4"
currentVal= "E4"

V = tDataBase[voltageVal].value
I = tDataBase[currentVal].value

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

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