🦄 The Fantasy Zookeeper (Solution)¶
Scenario:
Congratulations! You've been hired as a data analyst at Fantasy Zoo, the world's first zoo for mythical creatures. Your job is to analyze the data of the mythical creatures living at the zoo and provide insights to help improve their wellbeing.
You've been given a dataset containing the following columns:
creature_name
: Name of the creaturecreature_type
: Type of the creature (e.g., dragon, unicorn, etc.)age
: Age of the creature (in years)health_status
: Health status of the creature (Excellent, Good, Fair, Poor)magical_ability_score
: Score representing the creature's magical ability (0-100)
Exercise:
In order to organize the animals into different habitats, you've been asked to answer the fsome questions regarding this dataset:
In [1]:
Copied!
import pandas as pd
import numpy as np
# Define the number of creatures
num_creatures = 100
# Define the creature types
creature_types = ['Dragon', 'Unicorn', 'Griffin', 'Phoenix', 'Mermaid', 'Centaur']
# Generate random data
data = {
'creature_name': ['Creature' + str(i) for i in range(num_creatures)],
'creature_type': np.random.choice(creature_types, num_creatures),
'age': np.random.randint(1, 1000, num_creatures),
'health_status': np.random.choice(['Excellent', 'Good', 'Fair', 'Poor'], num_creatures),
'magical_ability_score': np.random.randint(0, 101, num_creatures)
}
# Create a DataFrame
df = pd.DataFrame(data)
import pandas as pd
import numpy as np
# Define the number of creatures
num_creatures = 100
# Define the creature types
creature_types = ['Dragon', 'Unicorn', 'Griffin', 'Phoenix', 'Mermaid', 'Centaur']
# Generate random data
data = {
'creature_name': ['Creature' + str(i) for i in range(num_creatures)],
'creature_type': np.random.choice(creature_types, num_creatures),
'age': np.random.randint(1, 1000, num_creatures),
'health_status': np.random.choice(['Excellent', 'Good', 'Fair', 'Poor'], num_creatures),
'magical_ability_score': np.random.randint(0, 101, num_creatures)
}
# Create a DataFrame
df = pd.DataFrame(data)
- Display the first 5 rows of the dataframe.
In [2]:
Copied!
df.head()
df.head()
Out[2]:
creature_name | creature_type | age | health_status | magical_ability_score | |
---|---|---|---|---|---|
0 | Creature0 | Centaur | 264 | Fair | 30 |
1 | Creature1 | Centaur | 865 | Excellent | 6 |
2 | Creature2 | Centaur | 956 | Fair | 22 |
3 | Creature3 | Griffin | 837 | Fair | 19 |
4 | Creature4 | Unicorn | 540 | Good | 6 |
- Calculate and print the total number of creatures in the zoo.
In [3]:
Copied!
total_creatures = df['creature_name'].count()
f'Total number of creatures: {total_creatures}'
total_creatures = df['creature_name'].count()
f'Total number of creatures: {total_creatures}'
Out[3]:
'Total number of creatures: 100'
- Calculate and print the average age of the creatures.
In [4]:
Copied!
average_age = df['age'].mean()
f'Average age of creatures: {average_age}'
average_age = df['age'].mean()
f'Average age of creatures: {average_age}'
Out[4]:
'Average age of creatures: 556.46'
- Find and print the name of the oldest creature.
In [5]:
Copied!
oldest_creature_index = df['age'].idxmax()
oldest_creature = df.loc[oldest_creature_index, 'creature_name']
f'Oldest creature: {oldest_creature}'
oldest_creature_index = df['age'].idxmax()
oldest_creature = df.loc[oldest_creature_index, 'creature_name']
f'Oldest creature: {oldest_creature}'
Out[5]:
'Oldest creature: Creature59'
- Identify and print the most common type of creature.
In [6]:
Copied!
common_creature_type = df['creature_type'].mode()[0]
f'Most common creature type: {common_creature_type}'
common_creature_type = df['creature_type'].mode()[0]
f'Most common creature type: {common_creature_type}'
Out[6]:
'Most common creature type: Dragon'
- Find and print the average magical ability score for each type of creature.
In [7]:
Copied!
average_ability_score = df.groupby('creature_type')['magical_ability_score'].mean()
average_ability_score
average_ability_score = df.groupby('creature_type')['magical_ability_score'].mean()
average_ability_score
Out[7]:
creature_type Centaur 45.052632 Dragon 54.500000 Griffin 55.818182 Mermaid 46.000000 Phoenix 64.416667 Unicorn 50.684211 Name: magical_ability_score, dtype: float64
- Find the names of creatures who have a 'Poor' health status.
In [8]:
Copied!
poor_health_creatures = df.loc[df['health_status'] == 'Poor', 'creature_name']
poor_health_creatures
poor_health_creatures = df.loc[df['health_status'] == 'Poor', 'creature_name']
poor_health_creatures
Out[8]:
11 Creature11 14 Creature14 15 Creature15 16 Creature16 19 Creature19 20 Creature20 23 Creature23 25 Creature25 38 Creature38 39 Creature39 46 Creature46 48 Creature48 50 Creature50 54 Creature54 55 Creature55 63 Creature63 65 Creature65 72 Creature72 77 Creature77 78 Creature78 86 Creature86 92 Creature92 93 Creature93 97 Creature97 Name: creature_name, dtype: object
Remember, the happiness of the mythical creatures is in your hands. Happy analyzing! ๐ช