Ordiance

cleanPokemonTable

Oct 22nd, 2023 (edited)
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. pokemonTable = pd.read_csv("dataRaw/pokemonRaw.csv")
  5.  
  6. pokemonTable = pokemonTable.drop(columns= ['against_bug', 'against_dark', 'against_dragon', 'against_electric',
  7.                     'against_fairy', 'against_fight', 'against_fire', 'against_fire', 'against_flying',
  8.                     'against_ghost', 'against_grass', 'against_ground', 'against_ground', 'against_ice',
  9.                     'against_normal', 'against_poison', 'against_psychic', 'against_rock', 'against_steel',
  10.                     'against_water', 'base_egg_steps', 'base_happiness','base_total', 'capture_rate',
  11.                      'experience_growth', 'height_m', 'percentage_male', 'pokedex_number'])
  12.  
  13. # From japanese_name column, extracts alphabetic characters into romanji column
  14. pokemonTable['romanji'] = pokemonTable['japanese_name'].str.extract(f'([A-Za-z]+)')
  15.  
  16. # Fill empty cells in weight_kg column with 0 values
  17. pokemonTable.weight_kg.fillna(0, inplace=True)
  18.  
  19.  
  20.  
  21. # Establish connection between abilities column and Abilities.csv
  22.  
  23. abilityTable = pd.read_csv("dataClean/Abilities.csv")
  24.  
  25. # Per row, replaces string storing all abilities into a list storing all abilities
  26. pokemonTable.abilities = pokemonTable.abilities.str.findall(pat="'([^']+)'")
  27.  
  28. # Creates a dictionary mapping the ability name to the index found in the Abilities.csv
  29. abilityIndexDict = dict()
  30. for index, ability in enumerate(abilityTable.Name):
  31.     # Remove spaces and capitalization to unify data
  32.     ability = ability.replace(" ", "").lower()
  33.     abilityIndexDict[ability] = index
  34.  
  35.  
  36. # Since there is a m:n relationship between Pokemon and Abilities, create a new table mapping every PokemonId to their corresponding AbilityIds
  37. pokemonAbilitiesTable = []
  38.  
  39. # Iterate through abilities column per Pokemon
  40. for i, abilities in enumerate(pokemonTable.abilities):
  41.     # Iterate through all possible abilities of one Pokemon
  42.     for ability in abilities:
  43.         # Translate abilityName to abilityIndex via abilityIndexDict
  44.         abilityIndex = abilityIndexDict[ability.replace(" ", "").lower()]
  45.         pokemonAbilitiesTable.append((i, abilityIndex))
  46.  
  47. # Create pokemonAbilitiesTable and store it as a separate CSV
  48. pokemonAbilitiesTable = pd.DataFrame(pokemonAbilitiesTable, columns=["PokemonId", "AbilityId"])
  49. pokemonAbilitiesTable.to_csv("dataClean/PokemonAbilitiesTable.csv")
  50.  
  51. # Drop abilities column since mapping has been extracted to separate PokemonAbilitiesTable
  52. pokemonTable.drop("abilities", axis=1, inplace=True)
  53.  
  54. pokemonTable = pokemonTable.drop(columns=['japanese_name'])
  55.  
  56.  
  57.  
  58.  
  59. # Move name-column to the start of the table
  60. col = pokemonTable.pop("name")
  61. pokemonTable.insert(0, col.name, col)
  62.  
  63. # Reorganize colummn order
  64. pokemonTable.rename({"classfication": "classification"}, axis=1, inplace=True)
  65. pokemonTable = pokemonTable[['name', 'romanji', 'type1', 'type2', 'classification', 'attack',
  66. 'defense', 'hp','sp_attack','sp_defense','speed','weight_kg','generation','is_legendary']]
  67.  
  68.  
  69.  
  70. pokemonTable.to_csv('dataClean/Pokemon.csv', index=False)
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment