---------------------------------------------------------------------------NameError Traceback (most recent call last)
CellIn[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'] = treturn pd.concat(dfs)
def get_data(tickers, period="1y"): dfs = [yf.Ticker(t).history(period=period) for t in tickers]for d, t inzip(dfs, tickers): d['Symbol'] = treturn 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
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)
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_changereturn 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()