{ "cells": [ { "cell_type": "markdown", "id": "68d30473-ca76-4f77-89db-dbd36b9fd859", "metadata": {}, "source": [ "# Fintech Pandas Challenge: Stock Portfolio Analysis\n", "\n", "### Description:\n", "You are a fintech analyst working for a investment firm. Your task is to analyze a stock portfolio and provide insights to the investment team. The portfolio consists of various stocks with their corresponding quantities and prices. Your goal is to calculate the total value of the portfolio, the average price of each stock, and the stock with the highest total value.\n", "\n", "Using Python and pandas, read the data into a DataFrame and perform the following tasks:\n", "- Calculate the total value of the portfolio by multiplying the quantity and price of each stock and summing them up.\n", "- Calculate the average price of each stock by dividing the total value of each stock by its quantity.\n", "- Determine the stock with the highest total value and display its symbol, quantity, price, and total value." ] }, { "cell_type": "markdown", "id": "a5a4e11e-4c36-4fd2-87f9-d754b27891eb", "metadata": {}, "source": [ "### Data:\n", "```\n", "Symbol\tQuantity\tPrice\n", "AAPL\t100\t145.23\n", "MSFT\t50\t234.56\n", "AMZN\t20\t3123.12\n", "GOOGL\t30\t2456.78\n", "FB\t40\t356.12\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "id": "75ec66ee-8bc0-4c33-9ee1-560a1d1ef428", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import sys" ] }, { "cell_type": "code", "execution_count": 2, "id": "d4b75bc8-80f0-4162-84e6-69386f974467", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python version 3.11.7 | packaged by Anaconda, Inc. | (main, Dec 15 2023, 18:05:47) [MSC v.1916 64 bit (AMD64)]\n", "Pandas version 2.2.1\n" ] } ], "source": [ "print('Python version ' + sys.version)\n", "print('Pandas version ' + pd.__version__)" ] }, { "cell_type": "code", "execution_count": 3, "id": "23e000e4-0f94-4c0c-9091-ce544285721a", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SymbolQuantityPrice
0AAPL100145.23
1MSFT50234.56
2AMZN203123.12
3GOOGL302456.78
4FB40356.12
\n", "
" ], "text/plain": [ " Symbol Quantity Price\n", "0 AAPL 100 145.23\n", "1 MSFT 50 234.56\n", "2 AMZN 20 3123.12\n", "3 GOOGL 30 2456.78\n", "4 FB 40 356.12" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.read_clipboard()\n", "df" ] }, { "cell_type": "code", "execution_count": 4, "id": "84ef15e7-dbc8-48ff-9a4f-a6877d669d0c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "RangeIndex: 5 entries, 0 to 4\n", "Data columns (total 3 columns):\n", " # Column Non-Null Count Dtype \n", "--- ------ -------------- ----- \n", " 0 Symbol 5 non-null object \n", " 1 Quantity 5 non-null int64 \n", " 2 Price 5 non-null float64\n", "dtypes: float64(1), int64(1), object(1)\n", "memory usage: 252.0+ bytes\n" ] } ], "source": [ "# verify data types\n", "df.info()" ] }, { "cell_type": "code", "execution_count": 5, "id": "6ca2fdcf-5607-40d1-b985-743762206ebe", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SymbolQuantityPriceTotal Value
0AAPL100145.2314523.0
1MSFT50234.5611728.0
2AMZN203123.1262462.4
3GOOGL302456.7873703.4
4FB40356.1214244.8
\n", "
" ], "text/plain": [ " Symbol Quantity Price Total Value\n", "0 AAPL 100 145.23 14523.0\n", "1 MSFT 50 234.56 11728.0\n", "2 AMZN 20 3123.12 62462.4\n", "3 GOOGL 30 2456.78 73703.4\n", "4 FB 40 356.12 14244.8" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# total value per Symbol\n", "df['Total Value'] = df['Quantity'] * df['Price']\n", "df" ] }, { "cell_type": "code", "execution_count": 6, "id": "12252187-f29d-4201-a30f-c7630ed153f9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "176661.59999999998" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calculate the total value of the portfolio\n", "total = df['Total Value'].sum()\n", "total" ] }, { "cell_type": "code", "execution_count": 7, "id": "4da1f11e-6118-4bf0-82cb-5af967cd441f", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SymbolQuantityPriceTotal ValueAvg Price
0AAPL100145.2314523.01216.426358
1MSFT50234.5611728.0753.161664
2AMZN203123.1262462.456.565742
3GOOGL302456.7873703.471.907782
4FB40356.1214244.8496.073234
\n", "
" ], "text/plain": [ " Symbol Quantity Price Total Value Avg Price\n", "0 AAPL 100 145.23 14523.0 1216.426358\n", "1 MSFT 50 234.56 11728.0 753.161664\n", "2 AMZN 20 3123.12 62462.4 56.565742\n", "3 GOOGL 30 2456.78 73703.4 71.907782\n", "4 FB 40 356.12 14244.8 496.073234" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# calculate the average price of each stock\n", "df['Avg Price'] = total/df['Price']\n", "df" ] }, { "cell_type": "code", "execution_count": 8, "id": "fd2664d0-0533-46ad-8276-920c02d15e24", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
SymbolQuantityPriceTotal ValueAvg Price
3GOOGL302456.7873703.471.907782
\n", "
" ], "text/plain": [ " Symbol Quantity Price Total Value Avg Price\n", "3 GOOGL 30 2456.78 73703.4 71.907782" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Determine the stock with the highest total value and display its symbol, quantity, price, and total value\n", "df.sort_values(by='Total Value', ascending=False).head(1)" ] }, { "cell_type": "markdown", "id": "318c5c0b-a99e-48cd-b5f3-d24fdd13b219", "metadata": {}, "source": [ "

This tutorial was created by HEDARO

" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.7" } }, "nbformat": 4, "nbformat_minor": 5 }