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
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
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...
Looking at my answers, I was am always off buy about range of .1 to .4...
Looking at my answers, I was am always off buy about range of .1 to .4 from the correct answer that is stated. I am having a really tough time trying to figure out how to get my number to be exactly like what is shown as correct so I need a little help with figuring out what I am doing wrong. Here is an example of a question I just cant seem to figure out. I'll show all my...
I have the below homework problem for a python programming class, and i really need some...
I have the below homework problem for a python programming class, and i really need some help!! First, take a set of 6 grades from a user and average them. Provide the average to the user. You need to check to make sure the grades are within the normal range. If the grade is less than 0 or more than 100, issue a warning to the user. You don't need to take the grade again, just let the user know....
(Python 3) If I have a list of keys and 3 lists of values, how can...
(Python 3) If I have a list of keys and 3 lists of values, how can I append these values into an existing key, value pair in a dictionary? Say I have: mykeys = ["John", "Sarah", "Lexi, "Cass"] values1 = [3, 5, 2, 6] values2 = [17, 18, 12, 21] values3 = [4, 7, 3, 0] How can I make it so my dictionary contains all three values in each of the keys. i.e.: {"John" : [3, 17, 4], "Sarah":...
I am trying to figure out how to find the test statistic using Excel. I understand...
I am trying to figure out how to find the test statistic using Excel. I understand the formula- that I need to use parameter 'p' and 'normal distribution' ….z= sample proportion and minus the 'population portion' then we divide p*q and divide n -sample size. I understand how to populate the data in the formula. I just can't figure out how to solve this using Excel. (my only option with my class) I need step by step directions if possible....
import pandas as pd import numpy as np import matplotlib.pyplot as plt mtcars = pd.read_csv("mtcars.csv", index_col...
import pandas as pd import numpy as np import matplotlib.pyplot as plt mtcars = pd.read_csv("mtcars.csv", index_col = 0) mtcars x = mtcars["hp"] y = mtcars["mpg"] plt.plot(x,y,"*") plt.grid(True) plt.xlabel("Horse Power") plt.ylabel("Miles per Gallon") plt.show() def standardize(x): return (x-x.mean())/x.std(), x.mean(), x.std() x, muX, stdX = standardize(x) y, muY, stdY = standardize(y) if len(x.shape) == 1: num_var = 1 else: num_var = x.shape[1] beta0 = np.random.rand() beta1 = np.random.rand() def predict(x, beta0, beta1): return beta0 + beta1*x def loss(y, ypred): return np.mean((y-ypred)**2)/2 def...
Using Python, write the following code. You are to allow the user to enter the daily...
Using Python, write the following code. You are to allow the user to enter the daily temperature as a floating-point number. You should make the assumption that you are recording temperatures in Fahrenheit. You should allow the user to continue entering temperatures until the value -999 is entered. This number should not be considered a temperature, but just a flag to stop the program. As the user enters a temperature, you should display the following each time: Current Temperature: 999...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT