Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Challenge: Calculate Room Statistics | Architectural Data Analysis with Python
Python for Architects

bookChallenge: Calculate Room Statistics

When you work with architectural data, you often need to extract meaningful statistics from schedules and tables. Imagine you are given a schedule of rooms in a building, each with its name, area, and occupancy. Your goal is to write a function that processes this data and provides useful insights for design or analysis. You will use a hardcoded pandas DataFrame to represent the room schedule and create a function that returns the total building area, the average room area, and a list of rooms where the occupancy is greater than 4. The function should return these results as a dictionary with the keys total_area, average_area, and high_occupancy_rooms.

To approach this challenge, start by importing the pandas library and constructing a DataFrame with the specified columns. Then, define a function that calculates the required statistics using pandas methods.

1234567891011121314151617181920212223
import pandas as pd # Hardcoded room schedule DataFrame data = { 'Room': ['Lobby', 'Conference', 'Office1', 'Office2', 'Storage', 'Lounge'], 'Area': [120.5, 85.0, 42.0, 39.5, 30.0, 55.5], 'Occupancy': [10, 8, 2, 2, 1, 5] } df = pd.DataFrame(data) def calculate_room_statistics(room_df): total_area = room_df['Area'].sum() average_area = room_df['Area'].mean() high_occupancy_rooms = room_df.loc[room_df['Occupancy'] > 4, 'Room'].tolist() return { 'total_area': total_area, 'average_area': average_area, 'high_occupancy_rooms': high_occupancy_rooms } # Example usage result = calculate_room_statistics(df) print(result)
copy

In this code, the DataFrame df contains the room schedule. The function calculate_room_statistics computes the total area using the sum() method, the average area using the mean() method, and filters rooms with occupancy greater than 4 using .loc[] and .tolist(). The result is a dictionary with the required statistics:

  • 'total_area': the sum of all values in the Area column;
  • 'average_area': the mean of all values in the Area column;
  • 'high_occupancy_rooms': a list of room names from the Room column where the corresponding Occupancy value is greater than 4.

This approach makes it easy to extract key insights from architectural data using pandas.

Tarea

Swipe to start coding

Write a function called calculate_room_statistics that takes a pandas DataFrame with columns Room, Area, and Occupancy, and returns a dictionary with:

  • The total area of all rooms, using the key total_area.
  • The average area of all rooms, using the key average_area.
  • A list of room names with occupancy greater than 4, using the key high_occupancy_rooms.

Test your function on this DataFrame:

import pandas as pd
df = pd.DataFrame({
    'Room': ['Studio', 'Office', 'Meeting', 'Break'],
    'Area': [40.0, 30.0, 50.0, 20.0],
    'Occupancy': [3, 2, 7, 5]
})

Your function should return:

{
    'total_area': 140.0,
    'average_area': 35.0,
    'high_occupancy_rooms': ['Meeting', 'Break']
}
"

Solución

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you explain how the function identifies high occupancy rooms?

What other statistics could I extract from this DataFrame?

Can you show how to modify the function for different occupancy thresholds?

close

bookChallenge: Calculate Room Statistics

Desliza para mostrar el menú

When you work with architectural data, you often need to extract meaningful statistics from schedules and tables. Imagine you are given a schedule of rooms in a building, each with its name, area, and occupancy. Your goal is to write a function that processes this data and provides useful insights for design or analysis. You will use a hardcoded pandas DataFrame to represent the room schedule and create a function that returns the total building area, the average room area, and a list of rooms where the occupancy is greater than 4. The function should return these results as a dictionary with the keys total_area, average_area, and high_occupancy_rooms.

To approach this challenge, start by importing the pandas library and constructing a DataFrame with the specified columns. Then, define a function that calculates the required statistics using pandas methods.

1234567891011121314151617181920212223
import pandas as pd # Hardcoded room schedule DataFrame data = { 'Room': ['Lobby', 'Conference', 'Office1', 'Office2', 'Storage', 'Lounge'], 'Area': [120.5, 85.0, 42.0, 39.5, 30.0, 55.5], 'Occupancy': [10, 8, 2, 2, 1, 5] } df = pd.DataFrame(data) def calculate_room_statistics(room_df): total_area = room_df['Area'].sum() average_area = room_df['Area'].mean() high_occupancy_rooms = room_df.loc[room_df['Occupancy'] > 4, 'Room'].tolist() return { 'total_area': total_area, 'average_area': average_area, 'high_occupancy_rooms': high_occupancy_rooms } # Example usage result = calculate_room_statistics(df) print(result)
copy

In this code, the DataFrame df contains the room schedule. The function calculate_room_statistics computes the total area using the sum() method, the average area using the mean() method, and filters rooms with occupancy greater than 4 using .loc[] and .tolist(). The result is a dictionary with the required statistics:

  • 'total_area': the sum of all values in the Area column;
  • 'average_area': the mean of all values in the Area column;
  • 'high_occupancy_rooms': a list of room names from the Room column where the corresponding Occupancy value is greater than 4.

This approach makes it easy to extract key insights from architectural data using pandas.

Tarea

Swipe to start coding

Write a function called calculate_room_statistics that takes a pandas DataFrame with columns Room, Area, and Occupancy, and returns a dictionary with:

  • The total area of all rooms, using the key total_area.
  • The average area of all rooms, using the key average_area.
  • A list of room names with occupancy greater than 4, using the key high_occupancy_rooms.

Test your function on this DataFrame:

import pandas as pd
df = pd.DataFrame({
    'Room': ['Studio', 'Office', 'Meeting', 'Break'],
    'Area': [40.0, 30.0, 50.0, 20.0],
    'Occupancy': [3, 2, 7, 5]
})

Your function should return:

{
    'total_area': 140.0,
    'average_area': 35.0,
    'high_occupancy_rooms': ['Meeting', 'Break']
}
"

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 3
single

single

some-alt