![](/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.