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?

python data-science data-analysis

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Exploratory Data Analysis is a significant part of Data Science

Data science is omnipresent to advanced statistical and machine learning methods. For whatever length of time that there is data to analyse, the need to investigate is obvious.

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

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

Python For Data Science | Python For Data Analysis

Python For Data Science | Python For Data Analysis - You will be working on an end-to-end case study to understand different stages in the Data science life cycle. This will deal with 'data manipulation' with pandas and Numpy,and 'data visualization' with Matplotlib. Learn about the basics of scikit-learn library to implement the machine learning algorithm.

Get started with Data Analysis and Data Science in Python and R 

Often people get frustrated when using a software for data analysis which is not particularly suitable for a given task but nevertheless.