[Python] How to read excel file with pandas
[Python] How to read excel file with pandas
How to read an excel file with pandas.
Official page
・ Pandas.read_excel
・ Xrld
**table of contents**
- [Install Library](Install # 1 Library)
- [Table to read](# 2 Table to read)
- [Read File](# 3 Read File)
- Default
- [Read by sheet number](#Read by sheet number)
- [Read by sheet name](#Read by sheet name)
- [Option](# 4 option)
## 1. 1. Library installation
Install the libraries needed to read the excel file.
Installation
pip install -U xlrd
pip install -U pandas
①xlrd
└ Library to read excel file
└「.xls」「.xlsx」
②pandas
└ Library for data analysis
About pip [here](https://qiita.com/yuta-38/items/730bf91526f92fe0b41a#3-pip%E3%81%AE%E4%B8%BB%E8%A6%81%E3%82% B3% E3% 83% 9E% E3% 83% B3% E3% 83% 89% E4% B8% 80% E8% A6% A7% E6% 97% A9% E8% A6% 8B% E8% A1% A8)
## 2. Table to read
・ Table including 3 sheets
-File path "~ / desktop / GA-demo.xlsx"
3. 3. Read file
① Default
② Read by sheet number
③ Read by sheet name
### ① Default
The first sheet is read.
Default
import pandas as pd
df = pd.read_excel('~/desktop/GA-demo.xlsx')
df
### ② Read by sheet number
Describe `sheet_name = n` as an option.
└ "n": Sheet number.
└ Sheet number starts from 0.
Specified by sheet number
import pandas as pd
pd.read_excel('~/desktop/GA-demo.xlsx', sheet_name=1)
### ③ Read by sheet name
Describe `sheet_name ='A'` as an option.
└ "A": Sheet name
Read by sheet name
import pandas as pd
pd.read_excel('~/desktop/GA-demo.xlsx', sheet_name='Data set 2')
## 4. option
Options are available when loading.
-Specify the row that will be the header (column name)
-Specify the column that will be the index (row name)
-Specify the number of lines to read
-Read the specified number of lines from the bottom
Etc. are possible.
Almost the same as the options when reading a csv file.
Click here for details (# https://qiita.com/yuta-38/items/e1e890a647e77c7ccaad)