Boolean indexing for quick stats
Let's return to the animals dataset, which is loaded as a list of dictionaries. You're going to use all you've learned and transform this data into a useable DataFrame, filter the data using Boolean indexing, and then perform some numpy magic to find out some interesting animal facts.
Este exercício faz parte do curso
Python for MATLAB Users
Instruções do exercício
- Create a DataFrame
animalsfrom the list of dictionariesanimals. - Create a Boolean index
mammalsby finding records where the "Class" is "Mammalia". - Create a Boolean index
birdsby finding records where the "Class" is "Aves". - Use
numpyto find the mean of the "Litter/Clutch size" column for mammals and birds.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Create a DataFrame from animals
animals = pd.____(animals)
# Create Boolean indices for mammals and birds
mammals = animals['Class']=='____'
birds = animals['Class']=='____'
# Use numpy and the Boolean indices to determine mean Litter/Clutch size
litter = np.____(animals[mammals]['Litter/Clutch size'])
clutch = np.____(animals[____]['Litter/Clutch size'])
# Print the average Litter/Clutch size of each class
print('Mammals have an average of {} offspring in each litter.'.format(round(litter, 2)))
print('The average clutch size in a single brood is {} eggs.'.format(round(clutch, 2)))