python: breaking age group by average number of friends

python: breaking age group by average number of friends

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

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?

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Python For Data Analysis | Build a Data Analysis Library from Scratch | Learn Python in 2019

Python For Data Analysis - Build a Data Analysis Library from Scratch - Learn Python in 2019

Data Science Course in Dallas

Become a data analysis expert using the R programming language in this [data science](https://360digitmg.com/usa/data-science-using-python-and-r-programming-in-dallas "data science") certification training in Dallas, TX. You will master data...

Python For Data Science - How to use Data Science with Python

This Edureka video on 'Python For Data Science - How to use Data Science with Python - Data Science using Python ' will help you understand how we can use python for data science along with various use cases. What is Data Science? Why Python? Python Libraries For Data Science. Roadmap To Data Science With Python. Data Science Jobs and Salary Trends