Question: I have this list that represent Fedex tracking

```
history = ['Tuesday, March 16, 2021', '3:03 PM Hollywood, FL\nDelivered\nLeft at front door. Signature Service not requested.', '5:52 AM MIAMI, FL\nOn FedEx vehicle for delivery', '5:40 AM MIAMI, FL\nAt local FedEx facility', 'Monday, March 15, 2021', '11:42 PM OCALA, FL\nDeparted FedEx location', '10:01 PM OCALA, FL\nArrived at FedEx location', '8:28 PM OCALA, FL\nIn transit', '12:42 AM OCALA, FL\nIn transit']

```

How do I transform this list into this 3 columns dataframe
[![enter image description here](https://i.stack.imgur.com/JbeGZ.png)](https://i.stack.imgur.com/JbeGZ.png)

Answer: 
```
history = [
    "Tuesday, March 16, 2021",
    "3:03 PM Hollywood, FL\nDelivered\nLeft at front door. Signature Service not requested.",
    "5:52 AM MIAMI, FL\nOn FedEx vehicle for delivery",
    "5:40 AM MIAMI, FL\nAt local FedEx facility",
    "Monday, March 15, 2021",
    "11:42 PM OCALA, FL\nDeparted FedEx location",
    "10:01 PM OCALA, FL\nArrived at FedEx location",
    "8:28 PM OCALA, FL\nIn transit",
    "12:42 AM OCALA, FL\nIn transit",
]

import re

r = re.compile("^(?:Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday)")

data, cur_group = [], ""
for line in history:
    if r.match(line):
        cur_group = line
    else:
        data.append([cur_group, *line.split("\n", maxsplit=1)])

df = pd.DataFrame(data)
print(df)

```

Prints:

```
                         0                      1                                                  2
0  Tuesday, March 16, 2021  3:03 PM Hollywood, FL  Delivered\nLeft at front door. Signature Servi...
1  Tuesday, March 16, 2021      5:52 AM MIAMI, FL                      On FedEx vehicle for delivery
2  Tuesday, March 16, 2021      5:40 AM MIAMI, FL                            At local FedEx facility
3   Monday, March 15, 2021     11:42 PM OCALA, FL                            Departed FedEx location
4   Monday, March 15, 2021     10:01 PM OCALA, FL                          Arrived at FedEx location
5   Monday, March 15, 2021      8:28 PM OCALA, FL                                         In transit
6   Monday, March 15, 2021     12:42 AM OCALA, FL                                         In transit

```
You can use [**`dateutil.parser.parse`**](https://dateutil.readthedocs.io/en/stable/parser.html) to check if an element is a valid datetime.

This should be safer than just checking if an element contains a day string (`Monday`, `Tuesday`, etc.) in case an event also contains a day string somewhere (e.g., `Delivery failed\nWill reattempt on Monday`).

```py
import dateutil.parser

history = ['Tuesday, March 16, 2021', '3:03 PM Hollywood, FL\nDelivered\nLeft at front door. Signature Service not requested.', '5:52 AM MIAMI, FL\nOn FedEx vehicle for delivery', '5:40 AM MIAMI, FL\nAt local FedEx facility', 'Monday, March 15, 2021', '11:42 PM OCALA, FL\nDeparted FedEx location', '10:01 PM OCALA, FL\nArrived at FedEx location', '8:28 PM OCALA, FL\nIn transit', '12:42 AM OCALA, FL\nIn transit']
data = []

for string in history:
    try:
        day = dateutil.parser.parse(string)
    except:
        data.append([day, *string.split('\n', maxsplit=1)])

df = pd.DataFrame(data)

#    0           1                      2
# 0  2021-03-16  3:03 PM Hollywood, FL  Delivered\nLeft at front door. Signature Servi...
# 1  2021-03-16  5:52 AM MIAMI, FL      On FedEx vehicle for delivery
# 2  2021-03-16  5:40 AM MIAMI, FL      At local FedEx facility
# 3  2021-03-15  11:42 PM OCALA, FL     Departed FedEx location
# 4  2021-03-15  10:01 PM OCALA, FL     Arrived at FedEx location
# 5  2021-03-15  8:28 PM OCALA, FL      In transit
# 6  2021-03-15  12:42 AM OCALA, FL     In transit

```