Week 1 Videos

These videos are intended to be watched between lecture on Wednesday and discussion section on Thursday. See Canvas for the accompanying video quizzes.

Find all numeric columns

  • Find all numeric columns in the file indexData.csv.

import pandas as pd
from pandas.api.types import is_numeric_dtype

df = pd.read_csv("../data/indexData.csv")
df.head()
Name Date Open High Low Close Adj Close Volume
0 NYA 12/31/65 528.690002 528.690002 528.690002 528.690002 528.690002 0.0
1 NYA 1/3/66 527.210022 527.210022 527.210022 527.210022 527.210022 0.0
2 NYA 1/4/66 527.840027 527.840027 527.840027 527.840027 527.840027 0.0
3 NYA 1/5/66 531.119995 531.119995 531.119995 531.119995 531.119995 0.0
4 NYA 1/6/66 532.070007 532.070007 532.070007 532.070007 532.070007 0.0
df.dtypes
Name          object
Date          object
Open         float64
High         float64
Low          float64
Close        float64
Adj Close    float64
Volume       float64
dtype: object
df["Open"]
0          528.690002
1          527.210022
2          527.840027
3          531.119995
4          532.070007
             ...     
112452    1241.119995
112453    1249.469971
112454    1256.079956
112455    1254.609985
112456    1258.489990
Name: Open, Length: 112457, dtype: float64
is_numeric_dtype(df["Open"])
True
is_numeric_dtype(df["Name"])
False
df.columns
Index(['Name', 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume'], dtype='object')
list(df.columns)
['Name', 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
[c for c in df.columns]
['Name', 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
list(c for c in df.columns)
['Name', 'Date', 'Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']
[len(c) for c in df.columns]
[4, 4, 4, 4, 3, 5, 9, 6]
[c for c in df.columns if len(c) == 4]
['Name', 'Date', 'Open', 'High']
[c for c in df.columns if is_numeric_dtype(df[c])]
['Open', 'High', 'Low', 'Close', 'Adj Close', 'Volume']

Display information using f-strings

  • For each row in the indexInfo.csv file, display the abbreviation and full name using f-strings.

import pandas as pd

df_info = pd.read_csv("../data/indexInfo.csv")
df_info
Region Exchange Index Currency
0 United States New York Stock Exchange NYA USD
1 United States NASDAQ IXIC USD
2 Hong Kong Hong Kong Stock Exchange HSI HKD
3 China Shanghai Stock Exchange 000001.SS CNY
4 Japan Tokyo Stock Exchange N225 JPY
5 Europe Euronext N100 EUR
6 China Shenzhen Stock Exchange 399001.SZ CNY
7 Canada Toronto Stock Exchange GSPTSE CAD
8 India National Stock Exchange of India NSEI INR
9 Germany Frankfurt Stock Exchange GDAXI EUR
10 Korea Korea Exchange KS11 KRW
11 Switzerland SIX Swiss Exchange SSMI CHF
12 Taiwan Taiwan Stock Exchange TWII TWD
13 South Africa Johannesburg Stock Exchange J203.JO ZAR
for i in df_info.index:
    print(i)
0
1
2
3
4
5
6
7
8
9
10
11
12
13
for i in df_info.index:
    abbrev = df_info.loc[i,"Index"]
    full = df_info.loc[i,"Exchange"]
    print(abbrev, full)
NYA New York Stock Exchange
IXIC NASDAQ
HSI Hong Kong Stock Exchange
000001.SS Shanghai Stock Exchange
N225 Tokyo Stock Exchange
N100 Euronext
399001.SZ Shenzhen Stock Exchange
GSPTSE Toronto Stock Exchange
NSEI National Stock Exchange of India
GDAXI Frankfurt Stock Exchange
KS11 Korea Exchange
SSMI SIX Swiss Exchange
TWII Taiwan Stock Exchange
J203.JO Johannesburg Stock Exchange
for i in df_info.index:
    abbrev = df_info.loc[i,"Index"]
    full = df_info.loc[i,"Exchange"]
    print(f"The abbreviation for {full} is {abbrev}")
The abbreviation for New York Stock Exchange is NYA
The abbreviation for NASDAQ is IXIC
The abbreviation for Hong Kong Stock Exchange is HSI
The abbreviation for Shanghai Stock Exchange is 000001.SS
The abbreviation for Tokyo Stock Exchange is N225
The abbreviation for Euronext is N100
The abbreviation for Shenzhen Stock Exchange is 399001.SZ
The abbreviation for Toronto Stock Exchange is GSPTSE
The abbreviation for National Stock Exchange of India is NSEI
The abbreviation for Frankfurt Stock Exchange is GDAXI
The abbreviation for Korea Exchange is KS11
The abbreviation for SIX Swiss Exchange is SSMI
The abbreviation for Taiwan Stock Exchange is TWII
The abbreviation for Johannesburg Stock Exchange is J203.JO
df_info.loc[2,"Index","Exchange"]
---------------------------------------------------------------------------
IndexingError                             Traceback (most recent call last)
/var/folders/8j/gshrlmtn7dg4qtztj4d4t_w40000gn/T/ipykernel_15841/2041133994.py in <module>
----> 1 df_info.loc[2,"Index","Exchange"]

~/miniconda3/envs/math10s22/lib/python3.7/site-packages/pandas/core/indexing.py in __getitem__(self, key)
    923                 with suppress(KeyError, IndexError):
    924                     return self.obj._get_value(*key, takeable=self._takeable)
--> 925             return self._getitem_tuple(key)
    926         else:
    927             # we by definition only have the 0th axis

~/miniconda3/envs/math10s22/lib/python3.7/site-packages/pandas/core/indexing.py in _getitem_tuple(self, tup)
   1101 
   1102         # no multi-index, so validate all of the indexers
-> 1103         self._has_valid_tuple(tup)
   1104 
   1105         # ugly hack for GH #836

~/miniconda3/envs/math10s22/lib/python3.7/site-packages/pandas/core/indexing.py in _has_valid_tuple(self, key)
    749         Check the key for valid keys across my indexer.
    750         """
--> 751         self._validate_key_length(key)
    752         for i, k in enumerate(key):
    753             try:

~/miniconda3/envs/math10s22/lib/python3.7/site-packages/pandas/core/indexing.py in _validate_key_length(self, key)
    790     def _validate_key_length(self, key: Sequence[Any]) -> None:
    791         if len(key) > self.ndim:
--> 792             raise IndexingError("Too many indexers")
    793 
    794     def _getitem_tuple_same_dim(self, tup: tuple):

IndexingError: Too many indexers
abbrev, full = df_info.loc[2,["Index","Exchange"]]
full
'Hong Kong Stock Exchange'
for i in df_info.index:
    abbrev, full = df_info.loc[i,["Index","Exchange"]]
    print(f"The abbreviation for {full} is {abbrev}")
The abbreviation for New York Stock Exchange is NYA
The abbreviation for NASDAQ is IXIC
The abbreviation for Hong Kong Stock Exchange is HSI
The abbreviation for Shanghai Stock Exchange is 000001.SS
The abbreviation for Tokyo Stock Exchange is N225
The abbreviation for Euronext is N100
The abbreviation for Shenzhen Stock Exchange is 399001.SZ
The abbreviation for Toronto Stock Exchange is GSPTSE
The abbreviation for National Stock Exchange of India is NSEI
The abbreviation for Frankfurt Stock Exchange is GDAXI
The abbreviation for Korea Exchange is KS11
The abbreviation for SIX Swiss Exchange is SSMI
The abbreviation for Taiwan Stock Exchange is TWII
The abbreviation for Johannesburg Stock Exchange is J203.JO