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.
# 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()
Get Answers For Free
Most questions answered within 1 hours.