Learn how to efficiently iterate over CSV rows, compare dates, and group them based on specific criteria using Python, with clear examples and code snippets. --- This video is based on the question https://stackoverflow.com/q/69921446/ asked by the user 'Chadi N' ( https://stackoverflow.com/u/13730807/ ) and on the answer https://stackoverflow.com/a/69930068/ provided by the user 'dawg' ( https://stackoverflow.com/u/298607/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Iterate over rows, compare dates and append to list Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Grouping Dates in CSV with Python Working with date data in CSV files can be challenging, especially when the goal is to compare those dates and group them based on specific criteria. In this article, we will explore how to iterate over rows in a CSV file, compare dates, and append these dates into groups based on their differences. We will provide a step-by-step solution using both standard Python libraries and Pandas. Problem Overview Suppose you have a CSV file structured with employee information that includes employee numbers, addresses, distances, and dates of inspections. Your goal is to group these date entries for each employee based on the following criteria: The dates that have at most a 90-day difference should be grouped together. All other dates should be placed in a separate group. Sample CSV Structure Here’s an example of our CSV contents: [[See Video to Reveal this Text or Code Snippet]] Expected Output Format For the provided CSV, the expected output grouping the dates would look like this: [[See Video to Reveal this Text or Code Snippet]] Solution Breakdown We will provide a solution using two different approaches: a non-Pandas approach and a Pandas approach. 1. Non-Pandas Solution Here's how you can achieve the desired grouping of dates without using Pandas: [[See Video to Reveal this Text or Code Snippet]] Explanation of the Non-Pandas Code Import Libraries: We start by importing necessary libraries for handling CSV, iterating groups, and manipulating dates. Read CSV: Using the csv module, we read and sort the data based on employee number and date. Group by Employee: We then group the sorted data by employee number and iterate through each group. Date Difference: We append dates within the 90-day range into subgroups and print the resulting groupings. 2. Pandas Solution For those who prefer using Pandas, here is the equivalent solution: [[See Video to Reveal this Text or Code Snippet]] Explanation of the Pandas Code Data Loading and Sorting: The CSV is read directly into a Pandas DataFrame, and sorted based on employee numbers and inspection dates. Grouping Logic: We create a new column to mark groups based on the date difference exceeding 90 days. Output: Finally, we iterate through the grouped data and print the employee number along with their corresponding grouped dates. Conclusion Grouping dates in a CSV file using Python is a straightforward process with both standard libraries and Pandas. Depending on your preference for using external libraries like Pandas or sticking to built-in functions, you can adapt the approach to your needs. By following the provided examples and explanations, you can efficiently handle and manipulate date data to best suit your requirements.