Question

In: Computer Science

Python. 5) What will the code below do? (Assume that we have a dataset df with...

Python.

5) What will the code below do? (Assume that we have a dataset df with these two columns named Occupation' and 'Age')

df.groupby('Occupation')['Age'].mean()

a) It will return the average age per occupation

b) It will return an error

c) It will return the total age per occupation

d) None of the options

6) df.describe() will return basic descriptive statistics only for numerical variables

True/False ?

7) Pandas dataframes can be converted into numpy arrays

Truse/False ?

Solutions

Expert Solution

Ans 5.)

The correct option is a) It will return the average age per occupation. (Attaching example reference)

Ans 6.)

The correct option is False. df.describe() can return statistics for other data types as well. (Attaching example reference)

Ans 7.)

The correct option is True. You can convert a dataframe to numpy arrays.

To convert a dataframe into a NumPy array you can use df.values. Add .values() with the rename_axis() function and you will get the desired result.

Below is the code for the same:-

import numpy as np
import pandas as pd
#defining index
index = [1, 2, 3, 4, 5, 6, 7]
#creating the rows
a = [np.nan, 0.1, np.nan, 0.1, 0.4, 0.1, 0.1]
b = [0.4, np.nan, 0.2, 0.2, 0.2, np.nan, np.nan]
c = [np.nan, 0.4, 0.5, np.nan, 0.6, 0.3, np.nan]
#defining the dataframe
df = pd.DataFrame({'A': a, 'B': b, 'C': c}, index=index)
#converting it to np arrays
df = df.rename_axis('ID').values
print(df)

Related Solutions

In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
What's wrong with my Python code. We have to find the regularexpression in Python My output...
What's wrong with my Python code. We have to find the regularexpression in Python My output should look like this My IP address 128. 0. 0. 1 My IP address 53. 20. 62. 201 My code ipAddresses = [] ipRegEx = re.compile(r"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")    result = myRegEx.find all(Search Text)    def Regular_expression(ipAddresses)       for i in ipAddresses:          print(i) ipSearchResult = ipRegEx.search(line)    if ipSearchResult != None and ipSearchResult.group() not in ipAddresses:       ipAddresses.append(ipSearchResult.group()) we have to use loop too.
In Python we will be using the below code for the problem: rainfile=open('rainfall.txt') for line in...
In Python we will be using the below code for the problem: rainfile=open('rainfall.txt') for line in rainfile: values = line.split() #print(values) print (values[0], 'had', values[1], 'inches of rain.') rainfile.close( ) The following is the text from rainfall.txt Akron 26.81 Albia 37.65 Algona 30.69 Allison 33.64 Alton 27.43 AmesW 34.07 AmesSE 33.95 Anamosa 35.33 Ankeny 33.38 Atlantic 34.77 Audubon 33.41 Beaconsfield 35.27 Bedford 36.35 BellePlaine 35.81 Belleveu 34.35 Blocton 36.28 Bloomfield 38.02 Boone 36.30 Brighton 33.59 Britt 31.54 Buckeye 33.66 BurlingtonKBUR...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000...
From the MNIST dataset introduced in class, write code in Python that a) Splits the 42000 training images into a training set (50% of all the data) and a test set (the rest). The labels should also be split accordingly. (PLEASE ONLY SOLVE 2 & 3) 2) Basically repeat Part 1, but now use 80% of the images for training and the other 20% for testing. Report scores. [10 points] 3) Use the SVM model from part 2 to print...
In the dataset airways (see R code), we have the change in airflow from moderate exercise...
In the dataset airways (see R code), we have the change in airflow from moderate exercise for 19 subjects under 2 different exposure conditions – regular air (air) and 0.25% sulpher dioxide (so2). a) Look at the correlation, and use the t-table to test the null hypothesis that air flow change under these two conditions is uncorrelated. Test at significance level 0.05. Show your work. b) Use a linear model and the summary function in R to test the null...
can you please convert this python code into java? Python code is as shown below: #...
can you please convert this python code into java? Python code is as shown below: # recursive function def row_puzzle_rec(row, pos, visited):    # if the element at the current position is 0 we have reached our goal    if row[pos] == 0:        possible = True    else:        # make a copy of the visited array        visited = visited[:]        # if the element at the current position has been already visited then...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in...
What does the RISC-V code below do? Write the C equivalent. Assume that i is in register x5 and that the base address of array A that holds doubleword integers is in x20. addi x5, x0, 0 addi x6, x0, 50 addi x28, x20, 0 loop: bge x5, x6, end ld x7, 0(x28) bge x7, x0, next sub x7, x0, x7 sd x7, 0(x28) next: addi x5, x5, 1 addi x28, x28, 8 jal x0, loop end: Can you rewrite...
Assume we have a dataset that includes 60 observations surrounding two variables of interest: (1) Soybean...
Assume we have a dataset that includes 60 observations surrounding two variables of interest: (1) Soybean yields in bushels per acre (bu/acre) and (2) fertilizer treatment. Variable (1) is quantitative while variable (2) is categorical; assume that there were four different fertilizer treatments tested. Assume also that the number of observations of each fertilizer treatment was the same for each group; i.e., 15 observations of each fertilizer treatment were collected. Write out the “Generic” null hypothesis. Write out the “Specific”...
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
hi i do not know what is wrong with my python code. this is the class:...
hi i do not know what is wrong with my python code. this is the class: class Cuboid: def __init__(self, width, length, height, colour): self.__width = width self.__length = length self.__height = height self.__colour = colour self.surface_area = (2 * (width * length) + 2 * (width * height) + 2 * (length * height)) self.volume = height * length * width def get_width(self): return self.__width def get_length(self): return self.__length def get_height(self): return self.__height def get_colour(self): return self.__colour def set_width(self,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT