简体   繁体   中英

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'. The values are only from 4th row and are in columns D and E. Lets say the values look like this:

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

Now, I just want to take out only the values starting with a voltage (V) of 45 and shouldnt take negative voltages . The corresponding current (I) values are also needed to be taken out. This has to be done for multiple excel files. 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:

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. So it will be really helpful if you guys could help. If there is a simplified versions with pandas it will be really great. Thank you in advance

The following uses pandas which you should definitly take a look at. 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.

The last line filters the 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

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

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