Đọc dữ liệu từ trang tính Excel

Nhập và quản lý dữ liệu tài chính bằng Python

Stefan Jansen

Instructor

Nhập dữ liệu từ Excel

excel.png

  • pd.read_excel(file, sheet_name=0)

    • Mặc định chọn trang đầu với sheet_name=0
    • Chọn theo tên với sheet_name='amex'
    • Nhập nhiều trang bằng danh sách như sheet_name=['amex', 'nasdaq']
Nhập và quản lý dữ liệu tài chính bằng Python

Nhập dữ liệu từ một trang

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

Nhập và quản lý dữ liệu tài chính bằng Python

Nhập dữ liệu từ hai trang

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
...
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy tên trang

xls = pd.ExcelFile('listings.xlsx') # đối tượng pd.ExcelFile

exchanges = xls.sheet_names
exchanges
['amex', 'nasdaq', 'nyse']
nyse = pd.read_excel(xls, 
                     sheet_name=exchanges[2],
                     na_values='n/a')
Nhập và quản lý dữ liệu tài chính bằng Python

Lấy tên trang

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
Nhập và quản lý dữ liệu tài chính bằng Python

Ayo berlatih!

Nhập và quản lý dữ liệu tài chính bằng Python

Preparing Video For Download...