Question: PYTHON I need to compute a 1D histogram out of the date data. From the entries, observe that the date is represented as dd.mm.yyyy (
PYTHON
I need to compute a 1D histogram out of the date data. From the entries, observe that the date is represented as dd.mm.yyyy (day, month, and year) and the data covers the earthquakes in between 02.12.2021 and 18.12.2021 (both included). Including the starting and the ending dates, our data covers 17 days in total.
Your first task is to count how many earthquakes per day and find the most active day in this interval. The build the histogram, you will allocate a bin for each day, i.e. the bin size will be one day. In the next step, you will count the number of earthquakes for each day. You can start from a list of zeros where the first index corresponds to the number of earthquakes that happened in 02.12.2021, the second index corresponds to 03.12.2021 and so on. After counting all of the entries in the input txt, the resulting histogram should be as follows:
[3, 56, 45, 53, 53, 33, 53, 44, 37, 36, 25, 37, 35, 32, 51, 41, 42]
The most active day is in the second index with 56 earthquakes, which correponds to the date 2021-12-03.
Please note that, using Python's datetime library is allowed. It is handy when you have data including different months. For example, you can calculate the difference like this:
import datetime # create a datetime object for the date 17.05.2020 x1 = datetime.datetime(2020, 5, 17) # create a datetime object for the date 19.06.2020 x2 = datetime.datetime(2020, 6, 19) # calculate the difference between x1 and x2 and print it in days print((x2-x1).day)
Hint: This may be useful for your histogram computation while calculating the index for a specific date.
Final remark: Your code should work with ANY date/time range, not just the dates in the given example.
The data is given from a 'data' list that consists of dictionaries, here is the ls the list:
data = [{'DATE': '2021.12.07', 'TIME': '22:39:59', 'LAT': '37.8525', 'LON': '26.9597', 'DEPTH': '25.2', 'MD': '-.-', 'MAG': '1.7', 'ML': '-.-', 'REGION': 'KUSADASI KORFEZI (AEGEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '22:19:52', 'LAT': '35.2473', 'LON': '25.4052', 'DEPTH': '8.1', 'MD': '-.-', 'MAG': '2.3', 'ML': '-.-', 'REGION': 'GIRIT ADASI (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '21:30:24', 'LAT': '38.7243', 'LON': '42.1942', 'DEPTH': '5.1', 'MD': '-.-', 'MAG': '1.7', 'ML': '-.-', 'REGION': 'GUZELSU-AHLAT (BITLIS)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '21:29:27', 'LAT': '39.7692', 'LON': '41.8692', 'DEPTH': '11.3', 'MD': '-.-', 'MAG': '2.0', 'ML': '-.-', 'REGION': 'SARITAS-KOPRUKOY (ERZURUM)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '20:32:21', 'LAT': '35.0387', 'LON': '25.3095', 'DEPTH': '10.6', 'MD': '-.-', 'MAG': '3.3', 'ML': '-.-', 'REGION': 'GIRIT ADASI (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '20:07:46', 'LAT': '38.8920', 'LON': '43.5583', 'DEPTH': '1.9', 'MD': '-.-', 'MAG': '2.4', 'ML': '-.-', 'REGION': 'COLPAN-(VAN)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '16:57:28', 'LAT': '37.7275', 'LON': '26.0377', 'DEPTH': '1.6', 'MD': '-.-', 'MAG': '2.6', 'ML': '-.-', 'REGION': 'ONIKI ADALAR (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '16:48:57', 'LAT': '38.7285', 'LON': '25.4705', 'DEPTH': '8.3', 'MD': '-.-', 'MAG': '1.8', 'ML': '-.-', 'REGION': 'AEGEAN SEA', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '16:29:05', 'LAT': '38.8595', 'LON': '27.1468', 'DEPTH': '9.6', 'MD': '-.-', 'MAG': '1.7', 'ML': '-.-', 'REGION': 'KAPIKAYA-ALIAGA (IZMIR)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '16:02:59', 'LAT': '41.1173', 'LON': '43.8922', 'DEPTH': '3.8', 'MD': '-.-', 'MAG': '2.5', 'ML': '-.-', 'REGION': 'ERMENISTAN', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '15:18:15', 'LAT': '38.8953', 'LON': '43.6128', 'DEPTH': '8.7', 'MD': '-.-', 'MAG': '2.3', 'ML': '-.-', 'REGION': 'SAGLAMTAS-(VAN)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '14:26:33', 'LAT': '38.8908', 'LON': '43.6298', 'DEPTH': '15.1', 'MD': '-.-', 'MAG': '2.0', 'ML': '-.-', 'REGION': 'SAGLAMTAS-(VAN)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '13:35:53', 'LAT': '38.9043', 'LON': '43.6152', 'DEPTH': '5.6', 'MD': '-.-', 'MAG': '1.4', 'ML': '-.-', 'REGION': 'SAGLAMTAS-(VAN)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '13:26:59', 'LAT': '38.8793', 'LON': '43.5043', 'DEPTH': '10.5', 'MD': '-.-', 'MAG': '2.3', 'ML': '-.-', 'REGION': 'ERMISLER-(VAN)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '13:07:49', 'LAT': '37.6938', 'LON': '25.9847', 'DEPTH': '10.8', 'MD': '-.-', 'MAG': '2.6', 'ML': '-.-', 'REGION': 'ONIKI ADALAR (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '12:39:52', 'LAT': '35.0968', 'LON': '25.2710', 'DEPTH': '6.2', 'MD': '-.-', 'MAG': '2.7', 'ML': '-.-', 'REGION': 'GIRIT ADASI (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}, {'DATE': '2021.12.07', 'TIME': '12:39:36', 'LAT': '37.6920', 'LON': '25.9977', 'DEPTH': '7.2', 'MD': '-.-', 'MAG': '2.7', 'ML': '-.-', 'REGION': 'ONIKI ADALAR (MEDITERRANEAN SEA)', 'METHOD': ' Quick'}]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
