Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre 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.

Tâche

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']
}
"

Solution

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
single

single

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

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

Glissez pour afficher le menu

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.

Tâche

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']
}
"

Solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 1. Chapitre 3
single

single

some-alt