Module III - Day 3

Python Foundation

Nov-Jan 2025 batch, Vikrant Patil

Date: 04 Mar 2026

Click here for All Notes

login to https://traininghub.vikrant.dev/ with your username and create a notebook with name module3-day3.ipynb

© Vikrant Patil

Stocks API

!pip install yfinance
Requirement already satisfied: yfinance in /home/vikrant/usr/local/default/lib/python3.13/site-packages (1.2.0)
Requirement already satisfied: pandas>=1.3.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (3.0.0)
Requirement already satisfied: numpy>=1.16.5 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (2.4.2)
Requirement already satisfied: requests>=2.31 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (2.32.5)
Requirement already satisfied: multitasking>=0.0.7 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (0.0.12)
Requirement already satisfied: platformdirs>=2.0.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (4.5.1)
Requirement already satisfied: pytz>=2022.5 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (2025.2)
Requirement already satisfied: frozendict>=2.3.4 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (2.4.7)
Requirement already satisfied: peewee>=3.16.2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (3.19.0)
Requirement already satisfied: beautifulsoup4>=4.11.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (4.14.3)
Requirement already satisfied: curl_cffi<0.14,>=0.7 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (0.13.0)
Requirement already satisfied: protobuf>=3.19.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (6.33.5)
Requirement already satisfied: websockets>=13.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from yfinance) (16.0)
Requirement already satisfied: cffi>=1.12.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from curl_cffi<0.14,>=0.7->yfinance) (2.0.0)
Requirement already satisfied: certifi>=2024.2.2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from curl_cffi<0.14,>=0.7->yfinance) (2025.11.12)
Requirement already satisfied: soupsieve>=1.6.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.8)
Requirement already satisfied: typing-extensions>=4.0.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from beautifulsoup4>=4.11.1->yfinance) (4.15.0)
Requirement already satisfied: pycparser in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from cffi>=1.12.0->curl_cffi<0.14,>=0.7->yfinance) (2.23)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0)
Requirement already satisfied: six>=1.5 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.17.0)
Requirement already satisfied: charset_normalizer<4,>=2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests>=2.31->yfinance) (3.4.4)
Requirement already satisfied: idna<4,>=2.5 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests>=2.31->yfinance) (3.11)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests>=2.31->yfinance) (2.6.2)
import yfinance as yf
reliance = yf.Ticker("RELIANCE.NS")
help(reliance.history)
Help on method history in module yfinance.base:

history(*args, **kwargs) -> pandas.DataFrame method of yfinance.ticker.Ticker instance
reliance.history()
Open High Low Close Volume Dividends Stock Splits
Date
2026-02-02 00:00:00+05:30 1340.000000 1392.900024 1335.500000 1390.400024 15711165 0.0 0.0
2026-02-03 00:00:00+05:30 1473.900024 1489.500000 1430.199951 1437.099976 25456171 0.0 0.0
2026-02-04 00:00:00+05:30 1444.000000 1464.000000 1440.300049 1456.800049 8333387 0.0 0.0
2026-02-05 00:00:00+05:30 1457.000000 1461.500000 1439.500000 1443.400024 11763658 0.0 0.0
2026-02-06 00:00:00+05:30 1441.500000 1452.800049 1433.500000 1450.800049 8277304 0.0 0.0
2026-02-09 00:00:00+05:30 1458.000000 1465.900024 1453.000000 1461.599976 5775864 0.0 0.0
2026-02-10 00:00:00+05:30 1471.000000 1471.000000 1452.199951 1458.500000 10259630 0.0 0.0
2026-02-11 00:00:00+05:30 1459.599976 1470.000000 1453.599976 1468.699951 7106877 0.0 0.0
2026-02-12 00:00:00+05:30 1470.000000 1473.000000 1445.500000 1448.900024 11135399 0.0 0.0
2026-02-13 00:00:00+05:30 1445.500000 1450.699951 1416.300049 1419.599976 10755693 0.0 0.0
2026-02-16 00:00:00+05:30 1420.099976 1439.599976 1409.300049 1437.099976 5896514 0.0 0.0
2026-02-17 00:00:00+05:30 1431.099976 1431.800049 1418.599976 1423.000000 10731630 0.0 0.0
2026-02-18 00:00:00+05:30 1427.000000 1442.099976 1419.199951 1441.300049 5521096 0.0 0.0
2026-02-19 00:00:00+05:30 1443.000000 1443.000000 1400.000000 1409.500000 11884664 0.0 0.0
2026-02-20 00:00:00+05:30 1409.500000 1427.400024 1406.099976 1419.400024 8177670 0.0 0.0
2026-02-23 00:00:00+05:30 1425.000000 1434.900024 1418.300049 1428.000000 7758856 0.0 0.0
2026-02-24 00:00:00+05:30 1425.300049 1433.300049 1415.000000 1428.800049 12529409 0.0 0.0
2026-02-25 00:00:00+05:30 1435.000000 1440.500000 1393.500000 1398.500000 10728792 0.0 0.0
2026-02-26 00:00:00+05:30 1398.500000 1412.900024 1391.900024 1406.800049 16683858 0.0 0.0
2026-02-27 00:00:00+05:30 1398.000000 1410.400024 1388.099976 1393.900024 12031440 0.0 0.0
2026-03-02 00:00:00+05:30 1375.500000 1378.599976 1341.500000 1358.000000 23728722 0.0 0.0
rel1yr = reliance.history(period="1y")
rel1yr
Open High Low Close Volume Dividends Stock Splits
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1425.300049 1433.300049 1415.000000 1428.800049 12529409 0.0 0.0
2026-02-25 00:00:00+05:30 1435.000000 1440.500000 1393.500000 1398.500000 10728792 0.0 0.0
2026-02-26 00:00:00+05:30 1398.500000 1412.900024 1391.900024 1406.800049 16683858 0.0 0.0
2026-02-27 00:00:00+05:30 1398.000000 1410.400024 1388.099976 1393.900024 12031440 0.0 0.0
2026-03-02 00:00:00+05:30 1375.500000 1378.599976 1341.500000 1358.000000 23728722 0.0 0.0

248 rows × 7 columns

type(rel1yr)
pandas.DataFrame
rel1yr.Close.std
<bound method Series.std of Date
2025-03-03 00:00:00+05:30    1166.590698
2025-03-04 00:00:00+05:30    1157.277954
2025-03-05 00:00:00+05:30    1170.923462
2025-03-06 00:00:00+05:30    1204.788208
2025-03-07 00:00:00+05:30    1244.828369
                                ...     
2026-02-24 00:00:00+05:30    1428.800049
2026-02-25 00:00:00+05:30    1398.500000
2026-02-26 00:00:00+05:30    1406.800049
2026-02-27 00:00:00+05:30    1393.900024
2026-03-02 00:00:00+05:30    1358.000000
Name: Close, Length: 248, dtype: float64>
infy1yr = yf.Ticker("INFY.NS").history(period="1y")
rel1yr
Open High Low Close Volume Dividends Stock Splits
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1425.300049 1433.300049 1415.000000 1428.800049 12529409 0.0 0.0
2026-02-25 00:00:00+05:30 1435.000000 1440.500000 1393.500000 1398.500000 10728792 0.0 0.0
2026-02-26 00:00:00+05:30 1398.500000 1412.900024 1391.900024 1406.800049 16683858 0.0 0.0
2026-02-27 00:00:00+05:30 1398.000000 1410.400024 1388.099976 1393.900024 12031440 0.0 0.0
2026-03-02 00:00:00+05:30 1375.500000 1378.599976 1341.500000 1358.000000 23728722 0.0 0.0

248 rows × 7 columns

infy1yr
Open High Low Close Volume Dividends Stock Splits
Date
2025-03-03 00:00:00+05:30 1643.657119 1678.913652 1643.657119 1659.488525 7504969 0.0 0.0
2025-03-04 00:00:00+05:30 1646.279465 1650.164490 1621.998057 1639.772095 6759673 0.0 0.0
2025-03-05 00:00:00+05:30 1643.802696 1683.138576 1643.802696 1662.305176 8180782 0.0 0.0
2025-03-06 00:00:00+05:30 1665.704607 1672.794826 1642.491557 1666.530151 10610547 0.0 0.0
2025-03-07 00:00:00+05:30 1654.146545 1655.991955 1627.339895 1637.538086 8019331 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1301.000000 1303.599976 1264.099976 1275.500000 27839061 0.0 0.0
2026-02-25 00:00:00+05:30 1286.199951 1323.599976 1286.199951 1290.099976 16669618 0.0 0.0
2026-02-26 00:00:00+05:30 1305.500000 1314.000000 1286.300049 1289.099976 11493914 0.0 0.0
2026-02-27 00:00:00+05:30 1326.300049 1335.000000 1294.199951 1300.099976 23141724 0.0 0.0
2026-03-02 00:00:00+05:30 1291.000000 1298.800049 1273.000000 1288.900024 9700361 0.0 0.0

248 rows × 7 columns

together = pd.concat([rel1yr, infy1yr])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[18], line 1
----> 1 together = pd.concat([rel1yr, infy1yr])

NameError: name 'pd' is not defined
import pandas as pd
together = pd.concat([rel1yr, infy1yr])
together
Open High Low Close Volume Dividends Stock Splits
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1301.000000 1303.599976 1264.099976 1275.500000 27839061 0.0 0.0
2026-02-25 00:00:00+05:30 1286.199951 1323.599976 1286.199951 1290.099976 16669618 0.0 0.0
2026-02-26 00:00:00+05:30 1305.500000 1314.000000 1286.300049 1289.099976 11493914 0.0 0.0
2026-02-27 00:00:00+05:30 1326.300049 1335.000000 1294.199951 1300.099976 23141724 0.0 0.0
2026-03-02 00:00:00+05:30 1291.000000 1298.800049 1273.000000 1288.900024 9700361 0.0 0.0

496 rows × 7 columns

We lost track of ticker!

rel1yr
Open High Low Close Volume Dividends Stock Splits
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1425.300049 1433.300049 1415.000000 1428.800049 12529409 0.0 0.0
2026-02-25 00:00:00+05:30 1435.000000 1440.500000 1393.500000 1398.500000 10728792 0.0 0.0
2026-02-26 00:00:00+05:30 1398.500000 1412.900024 1391.900024 1406.800049 16683858 0.0 0.0
2026-02-27 00:00:00+05:30 1398.000000 1410.400024 1388.099976 1393.900024 12031440 0.0 0.0
2026-03-02 00:00:00+05:30 1375.500000 1378.599976 1341.500000 1358.000000 23728722 0.0 0.0

248 rows × 7 columns

rel1yr['Symbol'] = 'RELIANCE.NS'
rel1yr
Open High Low Close Volume Dividends Stock Splits Symbol
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0 RELIANCE.NS
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0 RELIANCE.NS
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0 RELIANCE.NS
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0 RELIANCE.NS
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0 RELIANCE.NS
... ... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1425.300049 1433.300049 1415.000000 1428.800049 12529409 0.0 0.0 RELIANCE.NS
2026-02-25 00:00:00+05:30 1435.000000 1440.500000 1393.500000 1398.500000 10728792 0.0 0.0 RELIANCE.NS
2026-02-26 00:00:00+05:30 1398.500000 1412.900024 1391.900024 1406.800049 16683858 0.0 0.0 RELIANCE.NS
2026-02-27 00:00:00+05:30 1398.000000 1410.400024 1388.099976 1393.900024 12031440 0.0 0.0 RELIANCE.NS
2026-03-02 00:00:00+05:30 1375.500000 1378.599976 1341.500000 1358.000000 23728722 0.0 0.0 RELIANCE.NS

248 rows × 8 columns

infy1yr['Symbol'] = "INFY.NS"
both  = pd.concat([rel1yr, infy1yr])
both
Open High Low Close Volume Dividends Stock Splits Symbol
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0 RELIANCE.NS
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0 RELIANCE.NS
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0 RELIANCE.NS
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0 RELIANCE.NS
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0 RELIANCE.NS
... ... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 1301.000000 1303.599976 1264.099976 1275.500000 27839061 0.0 0.0 INFY.NS
2026-02-25 00:00:00+05:30 1286.199951 1323.599976 1286.199951 1290.099976 16669618 0.0 0.0 INFY.NS
2026-02-26 00:00:00+05:30 1305.500000 1314.000000 1286.300049 1289.099976 11493914 0.0 0.0 INFY.NS
2026-02-27 00:00:00+05:30 1326.300049 1335.000000 1294.199951 1300.099976 23141724 0.0 0.0 INFY.NS
2026-03-02 00:00:00+05:30 1291.000000 1298.800049 1273.000000 1288.900024 9700361 0.0 0.0 INFY.NS

496 rows × 8 columns

altair ploting

!pip install altair
Requirement already satisfied: altair in /home/vikrant/usr/local/default/lib/python3.13/site-packages (6.0.0)
Requirement already satisfied: jinja2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from altair) (3.1.6)
Requirement already satisfied: jsonschema>=3.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from altair) (4.25.1)
Requirement already satisfied: narwhals>=1.27.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from altair) (2.17.0)
Requirement already satisfied: packaging in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from altair) (25.0)
Requirement already satisfied: typing-extensions>=4.12.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from altair) (4.15.0)
Requirement already satisfied: attrs>=22.2.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from jsonschema>=3.0->altair) (25.4.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from jsonschema>=3.0->altair) (2025.9.1)
Requirement already satisfied: referencing>=0.28.4 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from jsonschema>=3.0->altair) (0.37.0)
Requirement already satisfied: rpds-py>=0.7.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from jsonschema>=3.0->altair) (0.30.0)
Requirement already satisfied: MarkupSafe>=2.0 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from jinja2->altair) (3.0.3)
import altair as alt
alt.Chart(rel1yr.reset_index()).mark_line().encode(
    x = 'Date',
    y = "Close"
)
alt.Chart(rel1yr.reset_index()).mark_circle().encode(
    x = 'Date',
    y = "Close"
)
alt.Chart(rel1yr.reset_index()).mark_bar().encode(
    x = 'Date',
    y = "Close"
)
alt.Chart(rel1yr.reset_index()).mark_area().encode(
    x = 'Date',
    y = "Close"
)
alt.Chart(both.reset_index()).mark_line().encode(
    x = 'Date',
    y = 'Close',
    color = 'Symbol'
)
def get_data(tickers, period="1y"):
    dfs = []
    for t in tickers:
        d = yf.Ticker(t).history(period=period)
        d['Symbol'] = t
    return pd.concat(dfs)
def get_data(tickers, period="1y"):
    dfs = [yf.Ticker(t).history(period=period) for t in tickers]
    for d, t in zip(dfs, tickers):
        d['Symbol'] = t
    return pd.concat(dfs)
data = get_data(['RELIANCE.NS','INFY.NS','TMPV.NS'])
data
Open High Low Close Volume Dividends Stock Splits Symbol
Date
2025-03-03 00:00:00+05:30 1199.210417 1201.650622 1151.401364 1166.590698 17944938 0.0 0.0 RELIANCE.NS
2025-03-04 00:00:00+05:30 1157.576688 1169.329796 1154.937327 1157.277954 11377373 0.0 0.0 RELIANCE.NS
2025-03-05 00:00:00+05:30 1156.381565 1178.294049 1152.397477 1170.923462 8664095 0.0 0.0 RELIANCE.NS
2025-03-06 00:00:00+05:30 1192.238355 1209.120879 1180.435518 1204.788208 14468014 0.0 0.0 RELIANCE.NS
2025-03-07 00:00:00+05:30 1211.162776 1249.808479 1207.178688 1244.828369 16474965 0.0 0.0 RELIANCE.NS
... ... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 376.850006 380.299988 371.649994 377.549988 7213746 0.0 0.0 TMPV.NS
2026-02-25 00:00:00+05:30 378.000000 385.600006 376.100006 381.850006 7748663 0.0 0.0 TMPV.NS
2026-02-26 00:00:00+05:30 381.700012 393.299988 380.100006 391.549988 14697480 0.0 0.0 TMPV.NS
2026-02-27 00:00:00+05:30 393.649994 394.049988 380.950012 382.649994 15951040 0.0 0.0 TMPV.NS
2026-03-02 00:00:00+05:30 358.000000 378.399994 355.500000 370.600006 11804900 0.0 0.0 TMPV.NS

590 rows × 8 columns

def lineplot_stocks(data, column):
    c = alt.Chart(data.reset_index()).mark_line().encode(
            x = 'Date',
            y = 'Close',
            color = 'Symbol'
    )
    return c
lineplot_stocks(data, 'Close')
yf.Ticker("TMPV.NS").history(period="1y")
TMPV.NS: Period '1yr' is invalid, must be one of: 1d, 5d, 1mo, 3mo, 6mo, ytd, max
Open High Low Close Adj Close Volume
Date
yf.Ticker("TMPV.NS").history(period="1y")
Open High Low Close Volume Dividends Stock Splits
Date
2025-10-15 00:00:00+05:30 403.000000 403.000000 388.000000 390.850006 26953550 0.0 0.0
2025-10-16 00:00:00+05:30 395.450012 400.450012 389.549988 396.799988 21983750 0.0 0.0
2025-10-17 00:00:00+05:30 396.799988 402.500000 392.250000 396.600006 16564540 0.0 0.0
2025-10-20 00:00:00+05:30 400.049988 403.399994 396.600006 399.750000 10471670 0.0 0.0
2025-10-21 00:00:00+05:30 402.799988 403.399994 400.049988 401.850006 3449156 0.0 0.0
... ... ... ... ... ... ... ...
2026-02-24 00:00:00+05:30 376.850006 380.299988 371.649994 377.549988 7213746 0.0 0.0
2026-02-25 00:00:00+05:30 378.000000 385.600006 376.100006 381.850006 7748663 0.0 0.0
2026-02-26 00:00:00+05:30 381.700012 393.299988 380.100006 391.549988 14697480 0.0 0.0
2026-02-27 00:00:00+05:30 393.649994 394.049988 380.950012 382.649994 15951040 0.0 0.0
2026-03-02 00:00:00+05:30 358.000000 378.399994 355.500000 370.600006 11804900 0.0 0.0

94 rows × 7 columns

!pip install nsetools
Collecting nsetools

  Downloading nsetools-2.0.1-py3-none-any.whl.metadata (15 kB)

Requirement already satisfied: six in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from nsetools) (1.17.0)

Collecting dateutils (from nsetools)

  Downloading dateutils-0.6.12-py2.py3-none-any.whl.metadata (1.3 kB)

Requirement already satisfied: requests in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from nsetools) (2.32.5)

Requirement already satisfied: python-dateutil in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from dateutils->nsetools) (2.9.0.post0)

Requirement already satisfied: pytz in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from dateutils->nsetools) (2025.2)

Requirement already satisfied: charset_normalizer<4,>=2 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests->nsetools) (3.4.4)

Requirement already satisfied: idna<4,>=2.5 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests->nsetools) (3.11)

Requirement already satisfied: urllib3<3,>=1.21.1 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests->nsetools) (2.6.2)

Requirement already satisfied: certifi>=2017.4.17 in /home/vikrant/usr/local/default/lib/python3.13/site-packages (from requests->nsetools) (2025.11.12)

Downloading nsetools-2.0.1-py3-none-any.whl (23 kB)

Downloading dateutils-0.6.12-py2.py3-none-any.whl (5.7 kB)

Installing collected packages: dateutils, nsetools

   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2/2 [nsetools]

Successfully installed dateutils-0.6.12 nsetools-2.0.1
import nsetools as nst
nse = nst.Nse() 
tickers= nse.get_stock_codes()
jitickers = [f"{j}.NS" for j in tickers if j.startswith("JI")]
jidata = get_data(jitickers)
lineplot_stocks(jidata, column="Close")

Example

calculate percentage change in prices for following tickers in year 2021

tickers_dict = {'INTC': 'Intel',
               'MSFT': 'Microsoft',
               'IBM': 'IBM',
               'BHP': 'BHP',
               'TM': 'Toyota',
               'AAPL': 'Apple',
               'AMZN': 'Amazon',
               'C': 'Citigroup',
               'QCOM': 'Qualcomm',
               'KO': 'Coca-Cola',
               'GOOG': 'Google'}
def fetch_data(tickers_dict, start, end):
    dfs = []
    for t, company in tickers_dict.items(): # we will iteartor over keys and values
        d = yf.Ticker(t).history(start=start, end=end)
        d['Symbol'] = company
        dfs.append(d)
    return pd.concat(dfs)
import datetime
start = datetime.datetime(2021, 1, 1)
end = datetime.datetime(2021, 12, 31)
data = fetch_data(tickers_dict, start, end)
d = yf.Ticker("INTC").history(start=start, end=end)
d
Open High Low Close Volume Dividends Stock Splits
Date
2021-01-04 00:00:00-05:00 45.101818 46.457856 44.658847 44.902931 46102500 0.0 0.0
2021-01-05 00:00:00-05:00 44.704054 45.951610 44.595572 45.752724 24866600 0.0 0.0
2021-01-06 00:00:00-05:00 45.599037 47.144923 45.382073 46.195694 36809600 0.0 0.0
2021-01-07 00:00:00-05:00 46.548269 47.633096 46.376502 47.181084 32092300 0.0 0.0
2021-01-08 00:00:00-05:00 47.416137 47.488457 46.349386 46.692917 34625400 0.0 0.0
... ... ... ... ... ... ... ...
2021-12-23 00:00:00-05:00 47.374982 47.977833 47.347156 47.588299 25198200 0.0 0.0
2021-12-27 00:00:00-05:00 47.625398 48.283902 47.579026 48.172604 20985400 0.0 0.0
2021-12-28 00:00:00-05:00 48.163328 48.339546 47.885089 48.005657 19290400 0.0 0.0
2021-12-29 00:00:00-05:00 48.052037 48.358099 47.912916 48.070587 14174800 0.0 0.0
2021-12-30 00:00:00-05:00 48.154055 48.358098 47.903642 47.987114 18359400 0.0 0.0

251 rows × 7 columns

data
Open High Low Close Volume Dividends Stock Splits Symbol
Date
2021-01-04 00:00:00-05:00 45.101818 46.457856 44.658847 44.902931 46102500 0.0 0.0 Intel
2021-01-05 00:00:00-05:00 44.704054 45.951610 44.595572 45.752724 24866600 0.0 0.0 Intel
2021-01-06 00:00:00-05:00 45.599037 47.144923 45.382073 46.195694 36809600 0.0 0.0 Intel
2021-01-07 00:00:00-05:00 46.548269 47.633096 46.376502 47.181084 32092300 0.0 0.0 Intel
2021-01-08 00:00:00-05:00 47.416137 47.488457 46.349386 46.692917 34625400 0.0 0.0 Intel
... ... ... ... ... ... ... ... ...
2021-12-23 00:00:00-05:00 145.994520 147.466588 145.856911 146.047134 13818000 0.0 0.0 Google
2021-12-27 00:00:00-05:00 146.365753 147.321581 146.153841 146.961777 13256000 0.0 0.0 Google
2021-12-28 00:00:00-05:00 147.269930 147.269930 144.849091 145.357773 18624000 0.0 0.0 Google
2021-12-29 00:00:00-05:00 145.339441 146.088068 144.421324 145.413879 17022000 0.0 0.0 Google
2021-12-30 00:00:00-05:00 145.359767 145.967710 144.673417 144.915604 12978000 0.0 0.0 Google

2761 rows × 8 columns

lineplot_stocks(data, column='Volume')
data.reset_index().groupby("Symbol").first()
Date Open High Low Close Volume Dividends Stock Splits
Symbol
Amazon 2021-01-04 00:00:00-05:00 163.500000 163.600006 157.201004 159.331497 88228000 0.0 0.0
Apple 2021-01-04 00:00:00-05:00 129.853862 129.941387 123.279474 125.856712 143301900 0.0 0.0
BHP 2021-01-04 00:00:00-05:00 43.681438 43.848454 43.148268 43.263893 3227471 0.0 0.0
Citigroup 2021-01-04 00:00:00-05:00 51.251057 51.350912 49.703290 50.044464 21286200 0.0 0.0
Coca-Cola 2021-01-04 00:00:00-05:00 46.691080 47.000805 44.763899 45.391953 25611100 0.0 0.0
Google 2021-01-04 00:00:00-05:00 87.222813 87.377158 84.756811 85.768723 38038000 0.0 0.0
IBM 2021-01-04 00:00:00-05:00 97.265757 97.319854 95.093991 95.789574 5417443 0.0 0.0
Intel 2021-01-04 00:00:00-05:00 45.101818 46.457856 44.658847 44.902931 46102500 0.0 0.0
Microsoft 2021-01-04 00:00:00-05:00 213.040133 213.490091 205.649354 208.406540 37130100 0.0 0.0
Qualcomm 2021-01-04 00:00:00-05:00 137.387108 139.308606 132.116461 133.337601 9827000 0.0 0.0
Toyota 2021-01-04 00:00:00-05:00 137.289771 137.289771 133.950273 135.069305 315600 0.0 0.0
data.reset_index().groupby("Symbol").last()
Date Open High Low Close Volume Dividends Stock Splits
Symbol
Amazon 2021-12-30 00:00:00-05:00 169.699997 170.888000 168.524002 168.644501 37584000 0.0 0.0
Apple 2021-12-30 00:00:00-05:00 175.618190 176.694587 174.267803 174.375443 59773000 0.0 0.0
BHP 2021-12-30 00:00:00-05:00 42.669834 42.782120 42.262786 42.290855 2306570 0.0 0.0
Citigroup 2021-12-30 00:00:00-05:00 51.984263 52.370158 51.787029 51.821331 11380500 0.0 0.0
Coca-Cola 2021-12-30 00:00:00-05:00 52.414075 52.564944 52.147831 52.165581 7703900 0.0 0.0
Google 2021-12-30 00:00:00-05:00 145.359767 145.967710 144.673417 144.915604 12978000 0.0 0.0
IBM 2021-12-30 00:00:00-05:00 113.566732 114.093168 113.210113 113.702591 3158100 0.0 0.0
Intel 2021-12-30 00:00:00-05:00 48.154055 48.358098 47.903642 47.987114 18359400 0.0 0.0
Microsoft 2021-12-30 00:00:00-05:00 330.072962 331.250726 327.089942 327.572632 15994500 0.0 0.0
Qualcomm 2021-12-30 00:00:00-05:00 169.977254 170.562499 166.712675 167.096741 4108400 0.0 0.0
Toyota 2021-12-30 00:00:00-05:00 166.870227 167.286784 166.363108 166.698166 123200 0.0 0.0
initial = data.reset_index().groupby("Symbol").first()
final = data.reset_index().groupby("Symbol").last()
percentage_change = (final.Close - initial.Close)/initial.Close*100
percentage_change
Symbol
Amazon        5.845049
Apple        38.550769
BHP          -2.249076
Citigroup     3.550576
Coca-Cola    14.922531
Google       68.960898
IBM          18.700383
Intel         6.868555
Microsoft    57.179632
Qualcomm     25.318545
Toyota       23.416764
Name: Close, dtype: float64
final['percentage_change'] = percentage_change
final
Date Open High Low Close Volume Dividends Stock Splits percentage_change
Symbol
Amazon 2021-12-30 00:00:00-05:00 169.699997 170.888000 168.524002 168.644501 37584000 0.0 0.0 5.845049
Apple 2021-12-30 00:00:00-05:00 175.618190 176.694587 174.267803 174.375443 59773000 0.0 0.0 38.550769
BHP 2021-12-30 00:00:00-05:00 42.669834 42.782120 42.262786 42.290855 2306570 0.0 0.0 -2.249076
Citigroup 2021-12-30 00:00:00-05:00 51.984263 52.370158 51.787029 51.821331 11380500 0.0 0.0 3.550576
Coca-Cola 2021-12-30 00:00:00-05:00 52.414075 52.564944 52.147831 52.165581 7703900 0.0 0.0 14.922531
Google 2021-12-30 00:00:00-05:00 145.359767 145.967710 144.673417 144.915604 12978000 0.0 0.0 68.960898
IBM 2021-12-30 00:00:00-05:00 113.566732 114.093168 113.210113 113.702591 3158100 0.0 0.0 18.700383
Intel 2021-12-30 00:00:00-05:00 48.154055 48.358098 47.903642 47.987114 18359400 0.0 0.0 6.868555
Microsoft 2021-12-30 00:00:00-05:00 330.072962 331.250726 327.089942 327.572632 15994500 0.0 0.0 57.179632
Qualcomm 2021-12-30 00:00:00-05:00 169.977254 170.562499 166.712675 167.096741 4108400 0.0 0.0 25.318545
Toyota 2021-12-30 00:00:00-05:00 166.870227 167.286784 166.363108 166.698166 123200 0.0 0.0 23.416764
alt.Chart(final.reset_index()).mark_bar().encode(
    x = 'Symbol',
    y = 'percentage_change')
def plot_percentage_change(data):
    initial = data.reset_index().groupby("Symbol").first()
    final = data.reset_index().groupby("Symbol").last()
    percentage_change = (final.Close - initial.Close)/initial.Close*100
    final['percentage_change'] = percentage_change
    return alt.Chart(final.reset_index()).mark_bar().encode(
        x = 'Symbol',
        y = 'percentage_change')
    
plot_percentage_change(data)

Yearwise percentage change

inputs
 - tickers
 - years

- get_data for given years
- compute percenatge change for each year

def get_data_yearwise(tickers, year):
    start = datetime.datetime(year, 1, 1)
    end = datetime.datetime(year, 12, 31)
    dfs = []
    for t, company in tickers.items():
        d = yf.Ticker(t).history(start=start, end=end)
        d['Symbol'] = company
        d['Year'] = year
        dfs.append(d)
    return pd.concat(dfs)


def get_data_all_years(tickers, years):
    dfs = [get_data_yearwise(tickers, y) for y in years]
    return pd.concat(dfs)

def compute_percentage(allyears_data):
    grpby = allyears_data.groupby(['Year', 'Symbol'])
    percentage = (grpby.Close.last() - grpby.Close.first()) / grpby.Close.first() * 100
    percentage.name = 'percentage'
    return percentage.reset_index()
tickers_dict
{'INTC': 'Intel',
 'MSFT': 'Microsoft',
 'IBM': 'IBM',
 'BHP': 'BHP',
 'TM': 'Toyota',
 'AAPL': 'Apple',
 'AMZN': 'Amazon',
 'C': 'Citigroup',
 'QCOM': 'Qualcomm',
 'KO': 'Coca-Cola',
 'GOOG': 'Google'}
years = list(range(2015, 2025))
years
[2015, 2016, 2017, 2018, 2019, 2020, 2021, 2022, 2023, 2024]
alldata = get_data_all_years(tickers_dict, years)
compute_percentage(alldata)
Year Symbol percentage
0 2015 Amazon 123.346944
1 2015 Apple -0.165979
2 2015 BHP -38.584500
3 2015 Citigroup -3.330121
4 2015 Coca-Cola 6.835213
... ... ... ...
105 2024 IBM 41.307768
106 2024 Intel -57.980238
107 2024 Microsoft 15.406165
108 2024 Qualcomm 12.407000
109 2024 Toyota 10.683975

110 rows × 3 columns

p = compute_percentage(alldata)
alt.Chart(p).mark_line().encode(
    x = 'Year:N',
    y = 'percentage',
    color = 'Symbol').properties(
    height = 400,
    width = 600)