Ordiance

extractClassifications

Oct 22nd, 2023
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. pokemonTable = pd.read_csv("dataClean/Pokemon.csv")
  5.  
  6. # Get a list of all unique values in the classification-column and sort it alphabetically
  7. classificationList = pokemonTable.classification.unique()
  8. classificationList = np.sort(classificationList)
  9.  
  10. # Build a dictionary from the list, mapping the name to the corresponding index
  11. classificationDict = {name: index for index, name in enumerate(classificationList)}
  12.  
  13. # Translate all names into numerical values using the created dictionary
  14. pokemonTable.classification = pokemonTable.classification.map(lambda x: classificationDict[x])
  15.  
  16. # Create new table with all index <-> name mappings
  17. classificationTable = pd.DataFrame(classificationDict.keys(), columns=["name"])
  18. # Cleanup name of row 331 because it contains a weird value ._.
  19. classificationTable.name.iloc[331] = "Mischief Pokémon"
  20.  
  21. # Replace every é with e because special character encoding in mySQL is hell ._.
  22. classificationTable.name.replace("é", "e", regex=True, inplace=True)
  23.  
  24. classificationTable.to_csv("dataClean/Classifications.csv", encoding="utf-8")
  25. pokemonTable.to_csv("dataClean/Pokemon.csv")
Advertisement
Add Comment
Please, Sign In to add comment