How to eliminate and treat data that does not meet the requirements in Excel data

Asked 1 years ago, Updated 1 years ago, 472 views

I'd like to read the specific sheet of Excel file (the name of the sheet has been decided) and determine whether the data should be imported.
Exclusion of nonconforming data (extracting suitable data) under the following conditions:
·Required items and blank spaces are not allowed
·No is greater than or equal to 1
·Model is not allowed except for the default string
·If the option string is not required when the type is C, NG

I used pandas to import Excel files.
It doesn't have to be especially pandas.

1) If we import it as DataFrame using Pandas, what should we do to condition it and extract it above?→Is there no other way but to condition it by turning the For statement?
2) For this purpose, would it be better not to release pandas+openpyxl?

Windows 10 python 3.9.10

US>pandas
openpyxl

Please interpret the [ ] part as blank on the excel sheet.

No, Name, Model, type, option
1, D001, [ ], A, [ ]
-1,D002,N-1,B,[ ]
2,D003,N-2,C,aaa

python pandas excel

2023-02-15 06:17

1 Answers

import pandas as pd

df = pd.read_excel('test.xlsx')

# Required Items
required = ['No', 'Name', 'type' ]
# Model—String specified
models = ['N-1', 'N-2' ]

idx=df[required].notna().all(axis=1)
dfx=df[idx].query('No>=1 and Model in @models and(type!="C" or option.notna())')')
print(dfx)

#    No Name Model type option
# 22 D003 N-2 Caaa


2023-02-15 16:09

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.