0% found this document useful (0 votes)
6 views3 pages

Stock Data Analysis with Pandas

The document outlines several Python scripts using pandas for data analysis on stock prices. It includes calculating the average close price, adding a percentage change column, identifying the symbol with the highest high price, displaying the first five rows of a dataset, and plotting a bar chart of daily price ranges. Each script includes error handling for file operations and data integrity checks.

Uploaded by

Manasa P M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Stock Data Analysis with Pandas

The document outlines several Python scripts using pandas for data analysis on stock prices. It includes calculating the average close price, adding a percentage change column, identifying the symbol with the highest high price, displaying the first five rows of a dataset, and plotting a bar chart of daily price ranges. Each script includes error handling for file operations and data integrity checks.

Uploaded by

Manasa P M
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. Calculate the average "Close Price" of all the symbol in the dataset.

import pandas as pd
df = pd.read_csv('C:/Users/HP/Downloads/[Link]')
average_close_price = df['Close Price'].mean()
print(f"The average Close Price is: {average_close_price}")

Output

The average Close Price is: 320.5509803921568

2. Add a new column called "Percentage Change" that represents the percentage change between
the "Open Price" and "Close Price" for each symbol.

import pandas as pd
df = pd.read_csv('C:/Users/HP/Downloads/[Link]')
df['Percentage Change'] = ((df['Close Price'] - df['Open Price']) / df['Open Price']) * 100
print(df)

Output

3. Determine which symbol had the highest "High Price" and what that price was

import pandas as pd
def highest_high_price(filepath):
try:
df = pd.read_csv(filepath)
max_high_row = df[df['High Price'] == df['High Price'].max()]
if not max_high_row.empty:
symbol = max_high_row['Symbol'].iloc[0]
high_price = max_high_row['High Price'].iloc[0]
print(f"The symbol with the highest 'High Price' is {symbol} with a price of {high_price}.")
else:
print("No data found or 'High Price' column is missing.")
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
except KeyError:
print("Error: 'Symbol' or 'High Price' columns not found in the CSV.")
except Exception as e:
print(f"An error occurred: {e}")
highest_high_price('C:/Users/HP/Downloads/[Link]')

Output

The symbol with the highest 'High Price' is AMZN with a price of 3450.0.

4. Load the [Link] file into a pandas DataFrame and display the first 5 rows

import pandas as pd
def display_first_5_rows(filepath):
try:
df = pd.read_csv(filepath)
print([Link](5)) # Display the first 5 rows
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
except Exception as e:
print(f"An error occurred: {e}")
display_first_5_rows('C:/Users/HP/Downloads/[Link]')

Output:

5. Data manipulation with Pandas, bar chart plotting with Matplotlib, sorting data.

import pandas as pd
import [Link] as plt
def price_range_bar_chart(filepath):
try:
df = pd.read_csv(filepath)
df['Price Range'] = df['High Price'] - df['Low Price']
df_sorted = df.sort_values(by='Price Range', ascending=False)
[Link](figsize=(12, 6))
[Link](df_sorted['Symbol'], df_sorted['Price Range'])
[Link]('Daily Price Range of Stocks (Sorted)')
[Link]('Stock Symbol')
[Link]('Price Range')
[Link](rotation=45, ha='right') # Rotate x-axis labels for readability
plt.tight_layout()
[Link]()
except FileNotFoundError:
print(f"Error: File not found at {filepath}")
except KeyError as e:
print(f"KeyError: Column '{[Link][0]}' not found in the CSV.")
except Exception as e:
print(f"An error occurred: {e}")
price_range_bar_chart('C:/Users/HP/Downloads/[Link]')

Output:

You might also like