In my code, the user inputs a text file. The text file contains 4 columns and the number of rows will vary with the text file that is loaded so the code must be generic. The first column of the array generated from the text file contains a type of animal, the second column is its Xlocation in a field, the third is its Ylocation in a field and the fourth is the animals Zlocation in the field. Load the data If you don't want to follow the link to the picture of the data, here is a copy of the code loading the data and the array that is returned:
#load the data
emplaced_animals_data = np.genfromtxt('animal_data.txt', skip_header = 1, dtype = str)
print(type(emplaced_animals_data))
print(emplaced_animals_data)
[['butterfly' '1' '1' '3']
['butterfly' '2' '2' '3']
['butterfly' '3' '3' '3']
['dragonfly' '4' '1' '1']
['dragonfly' '5' '2' '1']
['dragonfly' '6' '3' '1']
['cat' '4' '4' '2']
['cat' '5' '5' '2']
['cat' '6' '6' '2']
['cat' '7' '8' '3']
['elephant' '8' '9' '3']
['elephant' '9' '10' '4']
['elephant' '10' '10' '4']
['camel' '10' '11' '5']
['camel' '11' '6' '5']
['camel' '12' '5' '6']
['camel' '12' '3' '6']
['bear' '13' '13' '7']
['bear' '5' '15' '7']
['bear' '4' '10' '5']
['bear' '6' '9' '2']
['bear' '15' '13' '1']
['dog' '1' '3' '9']
['dog' '2' '12' '8']
['dog' '3' '10' '1']
['dog' '4' '8' '1']]
After the data is loaded in, there will always be two types of animals in the data that we don't want to know anything about so I remove the names of these animals from the first column, but I am unsure how to remove the data from the whole row. How would I extend the selection of data from the type of animal to its location and delete it for the unwanted animals? I have included images to show the outputs of what I have currently done. Remove Unwanted Animals
#Removes unwanted animals from list
print('Original list:', emplaced_animals_data[:,0])
all_the_animals = list(emplaced_animals_data[:,0])
Butterfly = set('butterfly')
Dragonfly = set('dragonfly')
for i in range(0, len(emplaced_animals_data)):
for animal in all_the_animals:
if Butterfly == set(animal):
all_the_animals.remove(animal)
if Dragonfly == set(animal):
all_the_animals.remove(animal)
print('Updated list:', words)
Next, I would like to take the remaining animals and sort each animal along with its location data into its own array which would be saved as some variable, but currently I am only able to sort the animal types into their own arrays. How would I extend my selection of the animals to incorporate their locations as well as save the animals and their locations to their own array based on type of animal?Grouping Animals
#Groups all of the items with the same name together
setofanimals = set(all_the_animals)
animal_groups = {}
for one in setofanimals:
ids = [one for i in emplaced_animals_data[:,0] if i == one]
animal_groups.update({one:ids})
for one in animal_groups:
print(one, ":", animal_groups[one])
My end goal is to be able to plot each occurrence of each type of animal regardless of the text file that is loaded in.
Here is the data I am working with, copied from the Excel Spreadsheet that I have saved as a text file: