Excel 워크시트에서 데이터 읽기

Python으로 금융 데이터 가져오기와 관리

Stefan Jansen

Instructor

Excel에서 데이터 가져오기

excel.png

  • pd.read_excel(file, sheet_name=0)

    • 기본으로 첫 시트 선택: sheet_name=0
    • 이름으로 선택: sheet_name='amex'
    • 여러 시트 가져오기: sheet_name=['amex', 'nasdaq']
Python으로 금융 데이터 가져오기와 관리

단일 시트에서 가져오기

amex = pd.read_excel('listings.xlsx',   
                     sheet_name='amex',   
                     na_values='n/a')

amex.info()
RangeIndex: 360 entries, 0 to 359
Data columns (total 7 columns):
 #   Column                 Non-Null Count  Dtype  
 --  ------                 --------------  -----  
 0   Stock Symbol           360 non-null    object 
 1   Company Name           360 non-null    object 
 2   Last Sale              346 non-null    float64
 3   Market Capitalization  360 non-null    float64
 4   IPO Year               105 non-null    float64

Python으로 금융 데이터 가져오기와 관리

두 시트에서 가져오기

listings = pd.read_excel('listings.xlsx',
                         sheet_name=['amex', 'nasdaq'],    # keys = sheet name    
                         na_values='n/a')                 # values = DataFrame 

listings['nasdaq'].info()
 #   Column                 Non-Null Count  Dtype  
 --  ------                 --------------  -----  
 0   Stock Symbol           3167 non-null   object 
 1   Company Name           3167 non-null   object 
 2   Last Sale              3165 non-null   float64
 3   Market Capitalization  3167 non-null   float64
 4   IPO Year               1386 non-null   float64
...
Python으로 금융 데이터 가져오기와 관리

시트 이름 가져오기

xls = pd.ExcelFile('listings.xlsx') # pd.ExcelFile object

exchanges = xls.sheet_names
exchanges
['amex', 'nasdaq', 'nyse']
nyse = pd.read_excel(xls, 
                     sheet_name=exchanges[2],
                     na_values='n/a')
Python으로 금융 데이터 가져오기와 관리

시트 이름 가져오기

nyse.info()
RangeIndex: 3147 entries, 0 to 3146
Data columns (total 7 columns):
 #   Column                 Non-Null Count  Dtype  
  --  ------                 --------------  -----  
 0   Stock Symbol           3147 non-null   object 
  1   Company Name           3147 non-null   object
 ...                        ... 
 6   Industry               2177 non-null   object 
dtypes: float64(3), object(4)
memory usage: 172.2+ KB
Python으로 금융 데이터 가져오기와 관리

연습해 봅시다!

Python으로 금융 데이터 가져오기와 관리

Preparing Video For Download...