Question

The below code will create a data fram for you. You need to first run the...

The below code will create a data fram for you.

You need to first run the below code and then answer the following questions.

# RUN THIS CELL AFTER ADDING YOUR STUDENT NUMBER

import pandas as pd

import numpy as np

my_student_number =

np.random.seed(my_student_number)

df = pd.DataFrame ({'action': ['BUY']*4+['SELL']*4,

                    'A': np.random.randint(20,45,8),

                    'B': np.random.randint(50,100,8),

                    'C': np.random.randint(110,140,8)})

The above df holds the buy/sell prices of three diferent stocks, A, B, and C recorded over a day. By assuming that you have already added your student number to the above code and run it, answer the below questions associated with operations over df.

## (8) Write a python code that returns the maximum stock price for each `action`.

(4) Write a python code that returns the value in the intersection of column 'A' and row index 5.

(4) What does this code do based on the above df?

(4) Write a python program that prints the sum of stocks of type A that are sold- i.e., their action is SELL.

Homework Answers

Answer #1
# RUN THIS CELL AFTER ADDING YOUR STUDENT NUMBER

import pandas as pd

import numpy as np

my_student_number = 20 # REPLACE WITH YOUR STUDENT NUMBER

np.random.seed(my_student_number)

df = pd.DataFrame ({'action': ['BUY']*4+['SELL']*4,

                    'A': np.random.randint(20,45,8),

                    'B': np.random.randint(50,100,8),

                    'C': np.random.randint(110,140,8)})


# Write a python code that returns the maximum stock price for each `action`.

# Creates a new columns with max stock price for that action
df['max_stock'] = df.apply(lambda x : max(x['A'], x['B'], x['C']), axis = 1)
df


# Write a python code that returns the value in the intersection of column 'A' and row index 5.
df.loc[5, 'A']



# PART 3 - WHICH CODE SNIPPET IS TO EXPLAINED IS NOT MENTIONED IN THE QUESTION ITSELF SO SKIPPING IT.



# Write a python program that prints the sum of stocks of type A that are sold- i.e., their action is SELL.
df.loc[df['action'] == 'SELL', 'A'].sum()

  

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
How to code this in python Write a program that computes and prints the first 1000...
How to code this in python Write a program that computes and prints the first 1000 prime numbers - simply write out each prime number on a new line. In implementing this solution I want you to define 2 functions: is_prime which can be used to test whether a number is a prime (e.g. is_prime(17) returns True but is_prime(9) returns False) and compute_primes which will return an array (or Python list) of the first n primes where n is passed...
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write...
****CODE IN PYTHON***** (Please replicate it EXACTLY as it is shown in the sample run) Write a program where the user and the program/computer play a number guessing game. The program should prompt the user for a number (between 1 and 100, inclusive) then the program/computer has to guess what the user entered. Keep track of the number of iterations it takes for the computer to guess the number. Sample runs are shown below: Enter number to be guessed: 88...
Write code snippets to do the following. You should write these in a Python editor and...
Write code snippets to do the following. You should write these in a Python editor and test them out as you go. When they work, copy and paste your final code snippet here. Define a function that takes a total bill and the number of people at your table, and prints the amount that each person owes. e.g. divide_bill(100, 5) would produce Each person owes $20 for their meal. e.g. divide_bill(50.25, 4)would produce Each person owes $12.56 for their meal....
For this lab assignment you will need to write some code and create some graphs. You...
For this lab assignment you will need to write some code and create some graphs. You may use excel to create your graphs, or other language of your choice. Your data needs to demonstrate the experimental running time for Selection Sort (code in book), Merge Sort (code in book), and the Arrays.sort() method. Here is a basic outline of how you need to code this assignment. 1) Create several arrays of size 100, 1000, 10000, 100000, 1000000, … (you need...
Summary In this lab, you use the pseudocode in figure below to add code to a...
Summary In this lab, you use the pseudocode in figure below to add code to a partially created Python program. When completed, college admissions officers should be able to use the Python program to determine whether to accept or reject a student, based on his or her class rank. start input testScore, classRank if testScore >= 90 then if classRank >= 25 then output "Accept" else output "Reject" endif else if testScore >= 80 then if classRank >= 50 then...
Using python, write the program below. Program Specifications: You are to write the source code and...
Using python, write the program below. Program Specifications: You are to write the source code and design tool for the following specification: A student enters an assignment score (as a floating-point value). The assignment score must be between 0 and 100. The program will enforce the domain of an assignment score. Once the score has been validated, the program will display the score followed by the appropriate letter grade (assume a 10-point grading scale). The score will be displayed as...
The code I have written runs but I need it us UDP not TCP. Also I...
The code I have written runs but I need it us UDP not TCP. Also I just need someone to check my work and make sure that it works properly. The python code needs to be run with command line so you can add more than one client. The directions: 1. The chat is performed between 2 clients and not the server. 2. The server will first start up and choose a port number. Then the server prints out its...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance:...
Assignment Statement Use the skeleton file starter code (below) to create the following classes using inheritance: ⦁   A base class called Pet ⦁   A mix-in class called Jumper ⦁   A Dog class and a Cat class that each inherit from Pet and jumper ⦁   Two classes that inherit from Dog: BigDog and SmallDog ⦁   One classes that inherit from Cat: HouseCat The general skeleton of the Pet, Dog, and BigDog classes will be given to you (see below, "Skeleton", but...
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately....
Java Code import java.util.Scanner; /** Create two methods as instructed below and then call them appropriately. Hint: Only one of the methods you create needs to be called from the main method. */ public class LandCalculation { public static void main(String[] args) { final int FEET_PER_ACRE = 43560; // Number of feet per acre double tract = 0.0, acres = 0.0; Scanner keyboard = new Scanner(System.in); System.out.print("Enter the tract size: "); tract = keyboard.nextDouble(); // Validate the user's input. while(tract...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT