Read the stock price data of Alphabet Inc. (GOOG) for the time period between April 01, 2020 and October 01, 2020 in the file 'alphabet.csv' into a Pandas DataFrame. You can download it from Blackboard in session 12. (1) Convert the “Date” column into Datetime format and assign it as the index column. And output the DataFram containing data for month x. (2) Calculate the net increase of stock price for each day and append them as a new column “Net Increase” to the DataFrame using the closing price minus the opening price. And output the DataFram containing data for month x. (3) Find the day that has the highest opening price, and calculate the mean of closing price for all days. NOTE:You should add the following code to your program to ensure that all columns are displayed when printing.Otherwise, the system will consider it as incorrect. pd.set_option('display.max_columns',1000) pd.set_option('display.width', 1000) pd.set_option('display.max_colwidth',1000) Input An integer x.
时间: 2024-01-31 12:02:20 浏览: 85
Sure, here's a Python program that will solve the problem:
```python
import pandas as pd
# Read the CSV file into a Pandas DataFrame and convert the "Date" column to Datetime format
df = pd.read_csv('alphabet.csv')
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# Define a function to calculate the net increase in stock price for each day
def calculate_net_increase(row):
return row['Close'] - row['Open']
# Add a new column to the DataFrame with the net increase for each day
df['Net Increase'] = df.apply(calculate_net_increase, axis=1)
# Get the input month from the user
x = int(input())
# Output the DataFrame for the input month, with all columns displayed
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
print("Data for month", x)
print(df[df.index.month == x])
# Find the day with the highest opening price and calculate the mean of the closing price for all days
highest_open = df['Open'].idxmax()
mean_close = df['Close'].mean()
print("Day with highest opening price:", highest_open.date())
print("Mean of closing price for all days:", mean_close)
```
To use this program, make sure that the 'alphabet.csv' file is in the same directory as your Python script. Then, simply run the program and input the desired month when prompted. The program will output the DataFrame containing data for the input month, as well as the day with the highest opening price and the mean of the closing price for all days.
Note that the program sets some display options for the Pandas DataFrame to ensure that all columns are displayed when printing. This is necessary to satisfy the requirements of the problem.
阅读全文