我有多個庫存表,如下所示:
| 行號 | -1 數量 | -2 數量 |
|---|---|---|
| 1 | - | 3 |
| 2 | 42.1 英尺 | - |
| 3 | 5 | - |
| 4 | - | 10英尺 |
| 5 | 2 | 1 |
| 6 | 6.7 | - |
或者
| 行號 | 數量 |
|---|---|
| 1 | 2 |
| 2 | 4.5公斤 |
| 3 | 5 |
| 4 | |
| 5 | 13 |
| 6 | 增強現實 |
我想使用 python 為數量列創建邏輯檢查。(該表可能有多個 qty 列,我需要能夠檢查所有列。在這兩個示例中,我將表格式化為資料框。)
可接受的標準:
- 帶或不帶“EA”的整數(表示每個)
- “AR”(根據需要)
- 具有測量單位的整數或浮點數
- 如果有多個 QTY 列,則也接受“-”(第一個表)
我想每頁回傳一個串列,其中包含行號。對應于數量值缺失的行(第 4 行,第二個表)或不符合驗收標準(第 6 行,表 1)。如果該行通過檢查,則回傳True。
我努力了:
qty_col = [col for col in df.columns if 'qty' in col]
df['corr_qty'] = np.where(qty_col.isnull(), False, df['line_no'])
但這會將數量列創建為串列并產生以下內容
AttributeError: 'list' object has no attribute 'isnull'
uj5u.com熱心網友回復:
介紹和建議:
歡迎來到 StackOverflow。在詢問 SO 問題時的一些一般提示包括盡可能多的資訊。此外,始終確定您要使用的庫和可接受的方法,因為同一問題可能有多種解決方案,看起來您已經這樣做了。
此外,最好始終分享所有嘗試過的解決方案(如果不是),以便其他人能夠理解思考程序并充分理解提供潛在解決方案的最佳方法。
解決方案:
目前尚不清楚您正在尋找的解決方案是否需要您閱讀 PDF 以創建資料框,或者是否將 PDF 轉換為 CSV 并使用 CSV 處理資料就足夠了。我采取了后一種方法。
import tabula as tb
import pandas as pd
#PDF file path
input_file_path = "/home/hackernumber7/Projects/python/resources/Pandas_Sample_Data.pdf"
#CSV file path
output_file_path = "/home/hackernumber7/Projects/python/resources/Pandas_Sample_Data.csv"
#Read the PDF
#id = tb.read_pdf(input_file_path, pages='all')
#Convert the PDF to CSV
cv = tb.convert_into(input_file_path, output_file_path, "csv", pages="all")
#Read initial data
id = pd.read_csv(output_file_path, delimiter=",")
#Print the initial data
print(id)
#Create the dataframe
df = pd.DataFrame(id, columns = ['qty'])
#Print the data as a DataFrame object; boolean values when conditions met
print(df.notna())
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/487587.html
