python: breaking age group by average number of friends

i have a dataframe of with 4 attributes, it can be seen blow.

what i wanted to do it that take the name and age of a person and count the number of friends he has. then of two ppl have the same age with different names, take the average number of friends for that age group. final divide the age range into age group and then take the average. this is how i tried.

#loc the attribute or features of interest
friends = df.iloc[:,3]
ages = df.iloc[:,2]

default of dictionary with age as key and value as a list of friends

dictionary_age_friends = defaultdict(list)

populating the dictionary with key age and values friend

for i,j in zip(ages,friends):
dictionary_age_friends[i].append(j)
print(“first dict”)
print(dictionary_age_friends)

#second dictionary, the same age is collected and the number of friends is added
set_dict ={}
for x in dictionary_age_friends:
list_friends =[]
for y in dictionary_age_friends[x]:
list_friends.append(y)
set_list_len = len(list_friends) # assign a friend with a number 1
set_dict[x] = set_list_len
print(set_dict)

set_dict ={}

for x in dictionary_age_friends:

print(“inside the loop”)

lis_1 =[]

for y in dictionary_age_friends[x]:

lis_1.append(y)

set_list = lis_1

set_list = [1 for x in set_list] # assign a friend with a number 1

set_dict[x] = sum(set_list)

a dictionary that assign the age range into age-groups

second_dict = defaultdict(list)
for i,j in set_dict.items():
if i in range(16,20):
i = ‘teens_youthAdult’
second_dict[i].append(j)
elif i in range(20,40):
i =“Adult”
second_dict[i].append(j)
elif i in range(40,60):
i =“MiddleAge”
second_dict[i].append(j)
elif i in range(60,72):
i = “old”
second_dict[i].append(j)
print(second_dict)
print(“final dict stared”)
new_dic ={}

for key,value in second_dict.items():
if key == ‘teens_youthAdult’:
new_dic[key] = round((sum(value)/len(value)),2)
elif key ==‘Adult’:
new_dic[key] = round((sum(value)/len(value)),2)
elif key ==‘MiddleAge’ :
new_dic[key] = round((sum(value)/len(value)),2)
else:
new_dic[key] = round((sum(value)/len(value)),2)
new_dic
end_time = datetime.datetime.now()

print(end_time-start_time)

print(new_dic)

some of the feedback i got is: 1, no need to build a list if u want just to count number of friends. 2, two ppl with the same age, 18. One has 4 friends, the other 3. the current code conclude that there are 7 average friends. 3, the code is not correct and optimal.

any suggestions or help? thanks indavance for all suggestion or helps?

#python #data-science #data-analysis

2 Likes5.90 GEEK