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. Output Task1: A DataFram containing data. Task2: A DataFram containing data. Task3: A datetime object. A float number(Keep six decimal places.).
时间: 2024-01-30 09:04:31 浏览: 94
Task1:
```python
import pandas as pd
# read in the data
df = pd.read_csv('alphabet.csv')
# convert the 'Date' column to datetime format and set it as the index
df['Date'] = pd.to_datetime(df['Date'])
df.set_index('Date', inplace=True)
# select data for month x (assuming x is an integer between 1 and 12)
x = 4
df_month_x = df[df.index.month == x]
# print the DataFrame
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
print(df_month_x)
```
Task2:
```python
# calculate the net increase of stock price for each day and append it as a new column
df['Net Increase'] = df['Close'] - df['Open']
# select data for month x (assuming x is an integer between 1 and 12)
x = 4
df_month_x = df[df.index.month == x]
# print the DataFrame
pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_colwidth', 1000)
print(df_month_x)
```
Task3:
```python
# find the day with the highest opening price
max_open = df['Open'].max()
max_open_date = df[df['Open'] == max_open].index[0]
# calculate the mean of closing price for all days
mean_close = df['Close'].mean()
# print the results
print(max_open_date)
print(round(mean_close, 6))
```
阅读全文