![](/img/trans.png)
[英]How to extract values in a specific row range from Excel with Python
[英]How to extract a particular set of values from excel file using a numerical range in python?
我打算做什么:
我有一个 excel 文件,其中包含我想从特定表格中提取的电压和电流数据,例如“IV_RAW”。 这些值仅来自第 4 行,位于 D 和 E 列中。假设这些值如下所示:
V(电压) | 我(电流) |
---|---|
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
的简化版本,那就太好了。 先感谢您
以下使用pandas
您一定要看看。 使用sheet_name
设置 sheet_name, header
是 header 的行索引(从 0 开始,因此行 4 -> 3), usecols
使用 A1 表示法定义列。
最后一行过滤 dataframe。 如果我理解正确,那么您希望电压在 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)]
在您的示例的基础上,您可以使用以下示例来获取您需要的内容
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]
你可以试试这个
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.