In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to deal with the grouping and aggregation of athlete information in Python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
1.1 data crawling
Code:
Import pandas as pdf = open ('athlete information table .csv') data=pd.read_csv (freguency skiprowswriting 0 headerbread 0) print (data)
Running result:
First of all, we use pd.read_csv to read the data, and convert the data into the format of dataframe to the object for initialization to facilitate the later analysis of the data.
1.2 Statistics on the average age, height and weight of men's and women's basketball players
Code:
Sex=data [[age (age) "," height (cm) "," weight (kg) "] .groupby (data [" gender "]) print (sex.mean ())
Running result:
First of all, we extract the data and make a grouping. First, we extract the three lines of data of "age (age)", "height (cm)" and "weight (kg)" and then group them according to gender.
Sex=data [[age (age) "," height (cm) "," weight (kg) "] .groupby (data [" gender "])
Then call mean () to calculate the average, and find out the average age, height and weight of men's and women's basketball players.
1.3 Statistics on the extreme differences of age, height and weight of male basketball players
Code:
Sex=data [["age (age)", "height (cm)", "weight (kg)"] .groupby (data ["gender"]) basketball_male=dict ([x for x in sex]) ['male'] basketball_male# to find the range def range_data_group (arr): return arr.max ()-arr.min () # to aggregate basketball_male.agg ({"age"): range_data_group, "height (cm)": range_data_group "kg": range_data_group})
Running result:
First, extract the data:
A single-line loop extracts data, and dict ([x for x in sex]) can simplify the writing of the for loop when there is only one line of statements in the loop. Define a function def range_data_group (arr): find the range
The solution to the range: use the maximum minus the minimum. You get very bad.
Agg () function: DataFrame.agg (* func*,*axis = 0cm ~ * args*,*** kwargs*) *
Func: function, function name, function list, dictionary {'row / column name', 'function name'}
Aggregates using one or more operations on the specified axis.
It should be noted that aggregate function operations are always performed on the axis (column axis by default, or row axis can be set), unlike numpy aggregate functions
Finally, we can get three columns of data: corresponding to "age", "height (cm)" and "weight (kg)".
1.4 Statistics on the body mass index of male basketball players
1.4.1 add body mass index
Code:
Data ["body mass index"] = 0data
Running result:
Add a row of body mass index: data ["body mass index"] = 0
1.4.2 calculate the BMI value and add data
Code:
# calculate the bmi value def outer (num): def bminum (sumbim): weight=data ["height (cm)"] height=data ["weight (kg)"] sumbim=weight/ (height/100) * * 2 return num+sumbim return bminum
Add this row of data to it:
Code:
# call function bimdata=data ["body mass index"] data ["body mass index"] = data [["body mass index"] .apply (outer (bimdata)) data
Running result:
Write a function to calculate the bmi value outer (num); then use apply's method to apply the custom function to the body mass index column. Then the value of the column is calculated and assigned.
Data ["BMI"] = data [["BMI"] .apply (outer (bimdata))
97622)]
Write a function to calculate the bmi value outer (num); then use apply's method to apply the custom function to the body mass index column. Then the value of the column is calculated and assigned.
Data ["BMI"] = data [["BMI"] .apply (outer (bimdata))
This is the end of the grouping and aggregation of information about how to deal with athletes in Python. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.