Question

In Python (using pandas and numpy) I have a CSV list (example.csv) and I am looking...

In Python (using pandas and numpy) I have a CSV list (example.csv) and I am looking to find the average age of the males in my data set. How would I make a function ('avg_age_males') that finds and outputs the average age for just the males in the data set? (In the .csv males is represented by 'M' and females is represented by 'F')

Homework Answers

Answer #1

1) Importing libraries

import pandas as pd
import numpy as np

2) Reading the .csv file

df = pd.read_csv("example.csv")
print(df)

3) Getting the male gender data

male = df[df.gender == 'm']
print(male)

4) Getting the age from the male dataframe

male_age = male['age']
print(male_age)

5) Calculating the mean

male_age.mean()

We can combine the points of 3, 4, and 5 into one single line

df[df.gender == 'm']['age'].mean()

I hope this answered your question. If you have any further doubts do let me know in the comment. I will incorporate the doubt in the answer. Regards.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In Python (using pandas and numpy) I am trying to clean CSV data so it adheres...
In Python (using pandas and numpy) I am trying to clean CSV data so it adheres to a strict coding system instead of free response. More specifically, how would I code a simple rule based system to handle the various spellings and word choices that represent the following statuses: Never married Divorced Married Widowed Separated
Using Python (pandas as pd) I am trying to use the split, apply, combine method but...
Using Python (pandas as pd) I am trying to use the split, apply, combine method but am getting an "invalid syntax" error at the end of the line that says "for days_name, days_df in grouped_by_day" I declared grouped_by_day in the split function but did not get any errors so I am not sure what happened or how to fix it. Below is a copy of the split, apply, combine section of my code. mean_data_ser = pd.Series() #split grouped_by_day= days_df.groupby("Day of...
0 I am using putty ssh to import my csv file to Hadoop file system (HDFS)....
0 I am using putty ssh to import my csv file to Hadoop file system (HDFS). So far I have made a directory using the command hadoop fs -mdkir /data after the directory I am trying to import my csv file using command: hadoop fs -cp s3://cis4567-fall19/Hadoop/SalesJan2 009.csv However I am getting a error that states : -cp: Not enough arguments: expected 2 but got 1
I am reading in a CSV file (using R). When I first check if there are...
I am reading in a CSV file (using R). When I first check if there are any NA's there are none. I then clean my data and convert my Income variable from num to factor by using this code to discretize income by equal-width bins: min_income <- min(bd$income) max_income <- max(bd$income) bins = 3 width=(max_income - min_income)/bins; bd$income = cut(bd$income, breaks=seq(min_income, max_income, width)) When I complete cleaning/updating my data and check again for NA's I receive one. It is specific...
I am working on a logistic regression model in python where I am using NBA data...
I am working on a logistic regression model in python where I am using NBA data to predict whether a player is a good rebounder or not based on various predictor variables. And my reg.coef is = array([[-1.50137324, -1.77554507, -1.73097902, 0.2568646 , 0.73556433, 0.00773832, -0.30204417, 0.26622955, 0.21256178]]) interpret the coefficients of your logistic regression model shown above
In python: I am trying to construct a list using enumerate and takewhile from a Fibonaci...
In python: I am trying to construct a list using enumerate and takewhile from a Fibonaci generator. So far the code I have is the following: def fibonacci(): (a, b) = (0, 1) while True: yield a (a, b) = (b, a + b) def createlist(n, fib): return [elem for (i, elem) in enumerate(takewhile(lambda x: x < n, fib)) if i < n] I only get half the list when I do: print(createlist(n, fibonacci())) Output: [0, 1, 1, 2, 3,...
This is my code, python. I have to search through the roster list to find a...
This is my code, python. I have to search through the roster list to find a player using their number. it says list index out of range. it also says there is error in my main. def file_to_dictionary(rosterFile): myDictionary={}       with open(rosterFile,'r') as f: data=f.read().split('\n')       for line in data:    (num,first,last,position)=line.split() myDict=[first, last, position] myDictionary[num]=myDict print (myDictionary) return myDictionary file_to_dictionary((f"../data/playerRoster.txt"))    def find_by_number(number): player=None    second=[] foundplayer= False myDictionary=file_to_dictionary((f"../data/playerRoster.txt")) for p in myDictionary: fullplayer=p.split() second.append([fullplayer[0], (fullplayer[1]+" "+...
Hello all. I have 3 questions concerning something in python and I wanted to make sure...
Hello all. I have 3 questions concerning something in python and I wanted to make sure I was correct seeing as to how this is my first python class. Consider the following code here: def make_plot(data, x_values, y_values, save_file=True): We just have to be able to point out the arguments, optional arguments, and required arguments. I think the arguments are : data, x_values, y_values, save_file = True I think the optional argument is: save_file = True Am I correct so...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was...
I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was looking at the solution and was able to understand what to do. However, when I implement the temp function as indicated, I keep getting this error "ValueError: the first two maketrans arguments must have equal length". However, it seems my two arguments are equal length, so I'm not sure what I am doing wrong! print('Exercise 5.30') def many(file): infile = open(file) content = infile.read()...
I am having a difficult time understanding what my professor is looking for in this research...
I am having a difficult time understanding what my professor is looking for in this research paper and he told me to reread his outline... I am at a loss of how to structure my research paper and would appreciate some quidence. Class: Healthcare Informatics Research paper topic: Information technology in public health disaster emergencies. Outline: -Your task is to produce a review of published literature (at least 70% of sources to be refereed journal papers, the rest can be...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT