I have a csv file that contains data like that
Sample csv
| Name | Start | End |
|---|---|---|
| John | 12:00 | 13:00 |
| John | 12:10 | 13:00 |
| John | 12:20 | 13:20 |
| Tom | 12:00 | 13:10 |
| John | 13:50 | 14:00 |
| Jerry | 14:00 | 14:30 |
| Alice | 15:00 | 16:00 |
| Jerry | 11:00 | 15:00 |
I need to perform Merging operation on overlapping intervals
Before merge
- John [12:00,13:00],[12:10,13:00],[12:20,13:20],[13:50,14:00]
- Jerry [14:00,14:35],[11:00,15:00]
- Tom [15:00,16:00]
- Alice [12:00,13:10]
After merge
- John [12:00,13:20],[13:50,14:00]
- Jerry [11:00,15:00]
- Alice [12:00 ,13:10]
- Tom [15:00,16:00]
with pandas its quite easily done. But i need to use it via python core libraries
I am tryring to do with csv_reader
import csv
dict = {}
with open('person.csv', mode='r') as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
name = row["Name"]
start_time = row["Start"]
end_time = row["End"]
dict[name] = [start_time,end_time]
but i am not sure how to compare the end and starting index to check overlapping condition
Sorting the data as a first step makes the computation easier. To avoid using index everywhere I chose to use a NamedTuple :