Sunday, 5 February 2023

Python Data Analysis

 

Real-world examples are an excellent way to learn about data analysis and to apply the concepts you have learned to real-life situations. In this tutorial, we will look at two common real-world examples: stock market data analysis and sales data analysis.

Google Colab Notebook is very handy for Data analysis. If you donot know about Google Colab then watch the video on youtube:  https://youtu.be/i-HnvsehuSw

In android phone or tablet just open chrome and check “Desktop Site” option in dropdown menu.

 

 

Before we dive into the examples, let's start by installing the necessary libraries. To install Pandas, you can use the following command:

pip install pandas

Next, let's import the necessary libraries and load the data into a Pandas data frame.


import pandas as pd
 
How to get data ?
 
        Let us download the stockmarket data of
        Tesla  from yahoofinance website.
        Go to website and type “TESLA”,then hit enter.
        The browser will take you to another window.
        Now select “Historical Data”. 
        A table will be displayed. 
        Just click “download”.
        Data will be downloaded into your devices 
        download folder.
        Now data can not accessible to 
        google colab.We have to downlod the data.
        In google colab select “files” option(not “file”).
        Click file download icon (the very first one). 
        Now file browser will appear.
        Locate downloaded file.
        Click on the file.
        A popup will appear.Click “OK”.
        Now data will appear in files.
        
 
 Now load the tesla data into a Pandas data frame
stock_data = pd.read_csv("TSLA.csv")
 
# Print the first five rows of the data frame
print(stock_data.head())

This should print out the first five rows of the stock market data.

Stock Market Data Analysis

Now that we have loaded the stock market data into a Pandas data frame, we can start analyzing it. Here are some common tasks you might perform when analyzing stock market data:

 Understood. Here's a revised version of the previous example, focusing on analyzing a single company's stock data over a 1-year period:

  1. Load the stock data into a Pandas data frame.
 
# Load the stock data into a Pandas data frame
stock_data = pd.read_csv("stock_data.csv")
 
# Print the first 5 rows of the data frame
print(stock_data.head())
  1. Plot the stock's opening, closing, high, and low prices over time.
 
import matplotlib.pyplot as plt
 
# Plot the stock's opening, closing, high, and low prices over time
plt.plot(stock_data['Date'], stock_data['Open'], label='Open')
plt.plot(stock_data['Date'], stock_data['Close'], label='Close')
plt.plot(stock_data['Date'], stock_data['High'], label='High')
plt.plot(stock_data['Date'], stock_data['Low'], label='Low')
 
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("Stock Price Over Time")
plt.legend()
plt.show()
  1. Calculate the daily returns for each day.
 
# Calculate the daily returns for each day
daily_returns = (stock_data['Close'] - stock_data['Open']) / stock_data['Open']
 
# Add the daily returns to the data frame
stock_data['daily_return'] = daily_returns
 
# Print the first 5 rows of the data frame
print(stock_data.head())
  1. Plot the daily returns over time.
 
# Plot the daily returns over time
plt.plot(stock_data['Date'], stock_data['daily_return'])
 
plt.xlabel("Date")
plt.ylabel("Daily Return")
plt.title("Daily Return Over Time")
plt.show()
  1. Calculate the average daily return.

# Calculate the average daily return
avg_daily_return = stock_data['daily_return'].mean()
 
# Print the average daily return
print("Average Daily Return:", avg_daily_return)

These examples should give you a good starting point for analyzing the stock data for a single company over a 1-year period. With Pandas and Python, the possibilities are endless, and you can explore even more in-depth analysis by adding additional calculations and plots.

 

Sales Data Analysis

Let's now look at another real-world example: sales data analysis. Here are some common tasks you might perform when analyzing sales data:

You can download data from Kaggle.

            Open Kaggle website in browser. You can sign in Kaggle with Gmail ID         .In data section search “Supermarket Sales”  and download it. Take datset into Google colab notebook.

 

 

Yes, let's continue with the sales data analysis example.

import pandas as pd

 

# read the csv file into a pandas dataframe

df = pd.read_csv(“supermarket_sales – Sheet1.csv”)

 

# display basic statistics of the dataframe

print("\nDataframe description:")

print(df.describe())

 

# group the data by 'Branch' and 'Product line' and find the sum of 'Total' sales

print("\nSales by Branch and Product line:")

sales_by_branch_product = df.groupby(['Branch', 'Product line'])['Total'].sum().reset_index()

print(sales_by_branch_product)

 

# find the top 5 branches with highest total sales

print("\nTop 5 branches with highest sales:")

top_5_branches = sales_by_branch_product.nlargest(5, 'Total')

print(top_5_branches)

 

# find the sum of 'Total' sales by 'Payment' type

print("\nSales by Payment type:")

sales_by_payment = df.groupby('Payment')['Total'].sum().reset_index()

print(sales_by_payment)

 

# find the sum of 'Total' sales by date

print("\nSales by date:")

  

In conclusion, real-world examples are a great way to learn about data analysis and to apply the concepts you have learned. Whether you are analyzing stock market data or sales data, Pandas and Python provide a powerful suite of tools for working with structured data.




Amelioration

This article was researched and written with the help of ChatGPT, a language model developed by OpenAI.

Special thanks to ChatGPT for providing valuable information and examples used in this article.

 

No comments:

Post a Comment