Pandas Datetime: Get the average mean of the UFO sighting was reported
Pandas Datetime: Exercise-17 with Solution
Write a Pandas program to get the average mean of the UFO (unidentified flying object) sighting was reported.
Sample Solution:
Python Code:
import pandas as pd
#Source: https://bit.ly/32kGinQ
df = pd.read_csv(r'ufo.csv')
df['date_documented'] = df['date_documented'].astype('datetime64[ns]')
print("Original Dataframe:")
print(df.head())
# Add a new column instance, this adds a value to each instance of ufo sighting
df['instance'] = 1
# set index to time, this makes df a time series df and then you can apply pandas time series functions.
df.set_index(df['date_documented'], drop=True, inplace=True)
# create another df by resampling the original df and counting the instance column by Month ('M' is resample by month)
ufo2 = pd.DataFrame(df['instance'].resample('M').count())
# just to find month of resampled observation
ufo2['date_documented'] = pd.to_datetime(ufo2.index.values)
ufo2['month'] = ufo2['date_documented'].apply(lambda x: x.month)
print("Average mean of the UFO (unidentified flying object) sighting was reported:")
print(ufo2.groupby(by='month').mean())
Sample Output:
Original Dataframe: Date_time city ... latitude longitude 0 06-01-1910 15:00 wills point ... 32.709167 -96.008056 1 06-11-1920 21:00 cicero ... 40.123889 -86.013333 2 07-05-1929 14:00 buchanan (or burns) ... 43.642500 -118.627500 3 06-01-1931 13:00 abilene ... 38.917222 -97.213611 4 06-01-1939 20:00 waterloo ... 34.918056 -88.064167 [5 rows x 11 columns] Average mean of the UFO (unidentified flying object) sighting was reported: instance month 1 2.875000 2 2.500000 3 1.764706 4 2.250000 5 1.375000 6 1.187500 7 1.562500 8 2.062500 9 1.312500 10 1.687500 11 1.437500 12 1.562500
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Pandas program to manipulate and convert date times with timezone information.
Next: Write a Pandas program to create a graphical analysis of UFO (unidentified flying object) Sightings year.
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
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework