I have a table with columns Date, Customer, SKU, enddate, Type, Comment. I need a…
- User
I have a table with columns Date, Customer, SKU, end_date, Type, Comment. I need a function to find rows where type = X, Comment is empty and Date = end_date for any not empty row for the same Customer and SKU. Write Python script
- Assistant
Certainly! Here's a Python script that defines a function called
find_rowswhich takes the table as input and returns the desired rows:pythondef find_rows(table): result = [] groups = {} for row in table: if row['Type'] == 'X' and row['Comment'] == '': key = (row['Customer'], row['SKU']) if key in groups and row['Date'] == groups[key]['end_date']: result.append(groups[key]) groups[key] = row return result - User
Note that comment and end_date are empty for type = X. Does it affect the code?
- Assistant
Apologies for the confusion. If the
Commentandend_datefields are empty for Type = X, we need to modify the code accordingly. Here's an updated version of the Python script:python