如何使用 python 中的数值范围从 excel 文件中提取一组特定的值?
How to extract a particular set of values from excel file using a numerical range in python?
我打算做什么:
我有一个包含电压和电流数据的 excel 文件,我想从特定的 sheet 说 '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
的简化版本,那将是非常棒的。
提前谢谢你
下面使用了pandas
,你一定要看看。使用 sheet_name
设置 sheet_name,header
是 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)]
你可以试试这个,
import openpyxl
tWorkbook = openpyxl.load_workbook("YOUR_FILEPATH")
tDataBase = tWorkbook.active
voltageVal= "D4"
currentVal= "E4"
V = tDataBase[voltageVal].value
I = tDataBase[currentVal].value
以您的示例为基础,您可以使用以下示例来获得所需的内容
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]
我打算做什么:
我有一个包含电压和电流数据的 excel 文件,我想从特定的 sheet 说 '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
的简化版本,那将是非常棒的。
提前谢谢你
下面使用了pandas
,你一定要看看。使用 sheet_name
设置 sheet_name,header
是 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)]
你可以试试这个,
import openpyxl
tWorkbook = openpyxl.load_workbook("YOUR_FILEPATH")
tDataBase = tWorkbook.active
voltageVal= "D4"
currentVal= "E4"
V = tDataBase[voltageVal].value
I = tDataBase[currentVal].value
以您的示例为基础,您可以使用以下示例来获得所需的内容
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]