Pandas Exercise

3.2. Pandas Exercise#

Create a DataFrame by reading the file ../../assets/data/Changi_daily_rainfall.csv. Perform the following two tasks with this DataFrame.

3.2.1. Task 1#

  1. Determine whether the dataframe contains NaN values. If so, fill the NaN values with the previous day’s value.

  2. Show the first 5 rows and the last 3 rows. Check the data length.

  3. For each year, calculate the frequency (i.e., number of days) of rainfall events whose magnitude exceeds 0.1 mm/day.

  4. Find the three months with the highest/lowest long-term averaged monthly rainfall. Are these months within your expection (e.g., Singapore’s monsoon seasons)?

Note: If you want to do this task on your local computer (not recommended), please modify the file path accordingly.

# Your solutions go here. Create a separate code chunk for each question.
# using the + icon to add a cell in the toolbar.

3.2.2. Task 2#

  1. Resample the DataFrame from daily time scale to annual time scale. Calculate the annual total rainfall for each year.

  2. Compare the long-term averaged annual rainfall in two periods: 1981-2000 and 2001-2020.

  3. Find the year with the largest/smallest annual rainfall and print their values.

  4. Calculate the trend of annual rainfall during 1981-2020 using the following formula:

\[a = \frac{n\sum{(xy)}-\sum{x}\sum{y}}{n\sum{x^2}-(\sum{x})^2}\]

where \(a\) is the trend of annual rainfall, \(x\) is year, \(y\) is annual rainfall, and \(n\) is the number of years.

# Your solutions go here. Create a separate code chunk for each question
# using the + icon to add a cell in the toolbar.