def aggregate_by_player_id(statistics, playerid, fields):
    """
    Inputs:
      statistics - List of batting statistics dictionaries
      playerid   - Player ID field name
      fields     - List of fields to aggregate
    Output:
      Returns a nested dictionary whose keys are player IDs and whose values
      are dictionaries of aggregated stats.  Only the fields from the fields
      input will be aggregated in the aggregated stats dictionaries.
    """

    aggregated_players = {}

    for stat in statistics:
        if stat[playerid] in aggregated_players:
            for field in fields:
                if field in aggregated_players[stat[playerid]]:
                    aggregated_players[stat[playerid]][field] += int(stat[field])
        else: 
            temp_dict = {}           
            for field in fields:                
                temp_dict[playerid] = stat[playerid]
                temp_dict[field] = int(stat[field])
            aggregated_players[stat[playerid]] = temp_dict
            
    return aggregated_players