Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Datetime: Get all the sighting days of the unidentified flying object (ufo) between 1950-10-10 and 1960-10-10

Pandas Datetime: Exercise-5 with Solution

Write a Pandas program to get all the sighting days of the unidentified flying object (ufo) between 1950-10-10 and 1960-10-10.

Sample Solution :

Python Code :

import pandas as pd
df = pd.read_csv(r'ufo.csv')
df['Date_time'] = df['Date_time'].astype('datetime64[ns]')
print("Original Dataframe:")
print(df.head())
print("\nSighting days of the unidentified flying object (ufo) between 1949-10-10 and 1960-10-10:")
selected_period = df[(df['Date_time'] >= '1950-01-01 00:00:00') & (df['Date_time'] <= '1960-12-31 23:59:59')]
print(selected_period)

Sample Output:

Original Dataframe:
            Date_time                  city     ...       latitude   longitude
0 1910-06-01 15:00:00           wills point     ...      32.709167  -96.008056
1 1920-06-11 21:00:00                cicero     ...      40.123889  -86.013333
2 1929-07-05 14:00:00  buchanan  (or burns)     ...      43.642500 -118.627500
3 1931-06-01 13:00:00               abilene     ...      38.917222  -97.213611
4 1939-06-01 20:00:00              waterloo     ...      34.918056  -88.064167

[5 rows x 11 columns]

Sighting days of the unidentified flying object (ufo) between 1949-10-10 and 1960-10-10:
             Date_time     ...       longitude
29 1950-06-01 16:00:00     ...      -89.116667
30 1950-06-01 20:00:00     ...      -79.996111
31 1950-08-01 04:00:00     ...      -85.759444
32 1950-10-01 11:00:00     ...      -82.518889
33 1951-06-01 07:00:00     ...      -99.950000
34 1951-07-01 03:00:00     ...     -117.105278
35 1951-02-03 22:00:00     ...      -72.599444
36 1951-06-03 13:00:00     ...      -77.206944
37 1952-07-01 15:00:00     ...      -95.088611
38 1952-07-01 22:00:00     ...      -83.045833
39 1952-08-01 21:30:00     ...      -82.458611
40 1952-10-01 12:00:00     ...      -94.578333
41 1953-04-01 15:00:00     ...      -71.077778
42 1953-04-01 18:00:00     ...      -71.106111
43 1953-07-01 05:30:00     ...     -104.820833
44 1953-08-01 12:00:00     ...      -90.331111
45 1954-02-01 02:00:00     ...     -147.716389
46 1954-06-01 00:00:00     ...      -95.363056
47 1954-06-01 06:00:00     ...      -76.823333
48 1954-06-01 08:00:00     ...      -89.643611
49 1955-05-01 15:00:00     ...      -71.009167
50 1955-06-01 02:00:00     ...      -95.398056
51 1955-06-01 15:29:00     ...      -84.456944
52 1955-06-01 17:00:00     ...     -122.133056
53 1956-01-01 05:30:00     ...      -80.589722
54 1956-03-01 13:00:00     ...     -122.635556
55 1956-05-01 12:00:00     ...      -81.378611
56 1956-06-01 19:00:00     ...      -94.531667
57 1957-01-01 21:00:00     ...      -96.800000
58 1957-05-01 12:00:00     ...      -81.378611
59 1957-06-01 10:00:00     ...     -106.486389
60 1957-06-01 20:00:00     ...      -73.644444
61 1958-01-01 22:00:00     ...     -102.557778
62 1958-06-01 02:00:00     ...      -78.204167
63 1958-06-01 19:00:00     ...     -122.418333
64 1958-06-01 21:00:00     ...      -74.006389
65 1959-04-01 01:00:00     ...      -80.193889
66 1959-05-01 18:30:00     ...      -82.998889
67 1959-06-01 12:00:00     ...      -73.026111
68 1959-06-01 18:30:00     ...      -84.155556
69 1960-02-01 22:15:00     ...      -93.093056
70 1960-02-01 23:00:00     ...      -82.932222
71 1960-04-01 21:00:00     ...      -95.363056
72 1960-05-01 20:00:00     ...     -110.925833

[44 rows x 11 columns]

Python Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to get all the sighting days of the unidentified flying object (ufo) which are less than or equal to 40 years (365*40 days).
Next: Write a Pandas program to get all the sighting years of the unidentified flying object (ufo) and create the year as column.

What is the difficulty level of this exercise?



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m