Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: Analyze Daily Temperature Data | Environmental Data Exploration
Python for Environmental Science

bookChallenge: Analyze Daily Temperature Data

In this challenge, you will analyze a set of daily temperature readings for a city over the course of one year. You will use a hardcoded pandas DataFrame to perform the analysis. Your objectives are to calculate the average, minimum, and maximum temperature for the year, identify the day or days with the highest and lowest temperatures, and then present these results in a format that is easy to interpret. This exercise will help you practice essential data exploration techniques that are foundational for environmental data analysis.

Start by importing the necessary library and creating a DataFrame containing daily temperature data. For demonstration purposes, a small sample dataset will be used, but the approach remains the same for larger datasets.

123456789101112131415
import pandas as pd # Sample data: daily temperatures for a week (replace with a full year for real analysis) data = { "date": [ "2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05", "2023-01-06", "2023-01-07" ], "temperature_C": [5.2, 6.1, 4.8, 7.3, 3.9, 8.0, 2.5] } df = pd.DataFrame(data) df["date"] = pd.to_datetime(df["date"]) print(df)
copy

With the data loaded into a DataFrame, you can now calculate the average, minimum, and maximum temperatures for the year by using pandas methods such as mean(), min(), and max() on the temperature_C column. This step provides a quick summary of the temperature distribution over the period.

12345678
# Calculate summary statistics average_temp = df["temperature_C"].mean() min_temp = df["temperature_C"].min() max_temp = df["temperature_C"].max() print(f"Average temperature: {average_temp:.2f}°C") print(f"Minimum temperature: {min_temp:.2f}°C") print(f"Maximum temperature: {max_temp:.2f}°C")
copy

To identify the day or days with the highest and lowest temperatures, use boolean indexing to filter the DataFrame for rows where the temperature matches the minimum or maximum values. This technique helps you pinpoint extreme events, which are often of particular interest in environmental studies.

1234567891011
# Find days with minimum temperature coldest_days = df[df["temperature_C"] == min_temp] # Find days with maximum temperature hottest_days = df[df["temperature_C"] == max_temp] print("Coldest day(s):") print(coldest_days[["date", "temperature_C"]]) print("Hottest day(s):") print(hottest_days[["date", "temperature_C"]])
copy

Presenting your results in a clear, readable format is crucial for effective communication. Use formatted print statements to summarize your findings, making it easy for others to understand the key insights from the data.

1234
print(f"Yearly Temperature Summary:") print(f"- Average: {average_temp:.2f}°C") print(f"- Minimum: {min_temp:.2f}°C on {', '.join(coldest_days['date'].dt.strftime('%Y-%m-%d'))}") print(f"- Maximum: {max_temp:.2f}°C on {', '.join(hottest_days['date'].dt.strftime('%Y-%m-%d'))}")
copy

Applying these techniques to a full year of daily temperature data will give you a comprehensive overview of the city's temperature patterns and help you identify notable weather events. This approach is fundamental in environmental science, as it allows you to summarize large datasets and focus on critical values and trends.

Now, put your skills to the test by analyzing a new set of daily temperature data. Your task is to use the provided DataFrame to calculate the average, minimum, and maximum temperature for the year, identify the day(s) with the highest and lowest temperatures, and output your results in a clear, readable format.

Tarefa

Swipe to start coding

  • Calculate the average, minimum, and maximum temperature for the year.
  • Identify the date(s) with the lowest and highest temperatures.
  • Print your results in a clear summary.

Solução

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

close

bookChallenge: Analyze Daily Temperature Data

Deslize para mostrar o menu

In this challenge, you will analyze a set of daily temperature readings for a city over the course of one year. You will use a hardcoded pandas DataFrame to perform the analysis. Your objectives are to calculate the average, minimum, and maximum temperature for the year, identify the day or days with the highest and lowest temperatures, and then present these results in a format that is easy to interpret. This exercise will help you practice essential data exploration techniques that are foundational for environmental data analysis.

Start by importing the necessary library and creating a DataFrame containing daily temperature data. For demonstration purposes, a small sample dataset will be used, but the approach remains the same for larger datasets.

123456789101112131415
import pandas as pd # Sample data: daily temperatures for a week (replace with a full year for real analysis) data = { "date": [ "2023-01-01", "2023-01-02", "2023-01-03", "2023-01-04", "2023-01-05", "2023-01-06", "2023-01-07" ], "temperature_C": [5.2, 6.1, 4.8, 7.3, 3.9, 8.0, 2.5] } df = pd.DataFrame(data) df["date"] = pd.to_datetime(df["date"]) print(df)
copy

With the data loaded into a DataFrame, you can now calculate the average, minimum, and maximum temperatures for the year by using pandas methods such as mean(), min(), and max() on the temperature_C column. This step provides a quick summary of the temperature distribution over the period.

12345678
# Calculate summary statistics average_temp = df["temperature_C"].mean() min_temp = df["temperature_C"].min() max_temp = df["temperature_C"].max() print(f"Average temperature: {average_temp:.2f}°C") print(f"Minimum temperature: {min_temp:.2f}°C") print(f"Maximum temperature: {max_temp:.2f}°C")
copy

To identify the day or days with the highest and lowest temperatures, use boolean indexing to filter the DataFrame for rows where the temperature matches the minimum or maximum values. This technique helps you pinpoint extreme events, which are often of particular interest in environmental studies.

1234567891011
# Find days with minimum temperature coldest_days = df[df["temperature_C"] == min_temp] # Find days with maximum temperature hottest_days = df[df["temperature_C"] == max_temp] print("Coldest day(s):") print(coldest_days[["date", "temperature_C"]]) print("Hottest day(s):") print(hottest_days[["date", "temperature_C"]])
copy

Presenting your results in a clear, readable format is crucial for effective communication. Use formatted print statements to summarize your findings, making it easy for others to understand the key insights from the data.

1234
print(f"Yearly Temperature Summary:") print(f"- Average: {average_temp:.2f}°C") print(f"- Minimum: {min_temp:.2f}°C on {', '.join(coldest_days['date'].dt.strftime('%Y-%m-%d'))}") print(f"- Maximum: {max_temp:.2f}°C on {', '.join(hottest_days['date'].dt.strftime('%Y-%m-%d'))}")
copy

Applying these techniques to a full year of daily temperature data will give you a comprehensive overview of the city's temperature patterns and help you identify notable weather events. This approach is fundamental in environmental science, as it allows you to summarize large datasets and focus on critical values and trends.

Now, put your skills to the test by analyzing a new set of daily temperature data. Your task is to use the provided DataFrame to calculate the average, minimum, and maximum temperature for the year, identify the day(s) with the highest and lowest temperatures, and output your results in a clear, readable format.

Tarefa

Swipe to start coding

  • Calculate the average, minimum, and maximum temperature for the year.
  • Identify the date(s) with the lowest and highest temperatures.
  • Print your results in a clear summary.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 1. Capítulo 3
single

single

some-alt