def avg_num_friends(graph):
    """Average number of friends in graph."""
    num_nodes = 0
    num_friends = 0
    for node in graph.nodes(data=True):
        if 'friends_total' in node[1]:
            num_friends += node[1]['friends_total']
            num_nodes += 1

    if num_nodes == 0:
        return 0
    else:
        return num_friends / num_nodes