<jupyter_start><jupyter_text>Ejercicio 1A partir de la API de Pokemon, se pide generar un DataFrame como el de la imagen con las columnas: - **"height","id","order","weight","types"** Ejercicio 2¿Has visto que dentro del DataFrame la columna "types" hay diccionarios? Se pide agregar, por cada pokemon, las columnas necesarias al DataFrame para albergar toda la información de "types".![](df_pk.PNG)<jupyter_code>import json
import requests

import pandas as pd
from pandas.io.json import json_normalize
url = "https://pokeapi.co/api/v2/pokemon"
poke_json = requests.get(url).json()
poke_json  
pokemon_url_list = []
url = 'https://pokeapi.co/api/v2/pokemon?offset=0&limit=5'

poke_json_5 = requests.get(url).json()
pokemon_url_list.extend(poke_json_5['results'])
pokemon_url_list

pokemon_url_df = pd.DataFrame(pokemon_url_list)
pokemon_url_df
#height, id, order, weight, types
url = 'https://pokeapi.co/api/v2/pokemon/1/'
pokemon_chars = requests.get(url).json()
pokemon_chars['types']
pokemon_url_df['url'][1]
height = []
id = []
order = []
weight = []
types = []

for url in pokemon_url_df['url']:
    pokemon_chars = requests.get(url).json()
    height.append(pokemon_chars['height'])
    id.append(pokemon_chars['id'])
    order.append(pokemon_chars['order'])
    weight.append(pokemon_chars['weight'])
    types.append(pokemon_chars['types'])

height = pd.DataFrame(height)
id = pd.DataFrame(id)
order = pd.DataFrame(order)
weight = pd.DataFrame(weight)
types = pd.DataFrame(types)

poke_chars_df = pd.concat([pokemon_url_df, height, id, order, weight, types], axis=1)
poke_chars_df
height = []
id = []
order = []
weight = []
types = []

for url in pokemon_url_df['url']:
    pokemon_chars = requests.get(url).json()
    height.append(pokemon_chars['height'])
    id.append(pokemon_chars['id'])
    order.append(pokemon_chars['order'])
    weight.append(pokemon_chars['weight'])
    types.append(pokemon_chars['types'])

chars_dict ={'height':height, 'id':id, 'order':order, 'weight': weight, 'types':types}
chars_df = pd.DataFrame(chars_dict)

poke_chars_df = pd.concat([pokemon_url_df, chars_df], axis=1)
poke_chars_df
poke_chars_df['types']
#types_string = json.dumps(poke_chars_df['types'].to_json())
types_string = poke_chars_df['types'].to_json()
#types_df = pd.DataFrame(types_string)
#types_df
#type(types_string)
types_string
types_string = poke_chars_df['types'].to_list()
types_df = pd.DataFrame(types_string)
types_df
poke_chars_df.loc[:,'types':]

type(poke_chars_df.loc[:,'types':])
for i, r in poke_chars_df.loc[:,'types':].iterrows():
    if i == 0:
        types_df = pd.DataFrame(r)
        print(r)
        print(type(r))  
    else:
        types_df = pd.concat([types_df, pd.DataFrame(r)], axis=0)
        

types_df
df_final = None
for i, x in enumerate(poke_chars_df['types']):
    if i == 0:
        df_final = pd.DataFrame(x)
    else:
        df_final = pd.concat([df_final, pd.DataFrame(x)], axis=0)
df_final
poke_chars_df.loc[:,'types':].types<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15492021276595744, 'fraction_numerical': 0.005651595744680851, 'mean_word_length': 5.415778251599147, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '562592', 'license': 'Apache-2.0', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1149, 'n_words': 208, 'path': '2_Ejercicios/Primera_parte/Entregables/2. Ejercios_Mod1/2020_12_16/api_pokemon_dataframe.ipynb', 'repo_name': 'JaunIgciona/DataBootcamp_Nov2020', 'size': '3008'}