python如何读取excel的一个sheet_python pandas是如何读取excel表中的sheet的(四)
上一期中,我們重點介紹了pandas中read_excel()中的index_col參數,本期介紹一下usecols參數。官方文檔說明:(支持int,str,以及他們的列表,還支持函數調用,默認None解讀所有列)usecols : int, str, list-like, or callable default NoneReturn a subset of the columns.
* If None, then parse all columns.
* If int, then indicates last column to be parsed.
.. deprecated:: 0.24.0
Pass in a list of int instead from 0 to `usecols` inclusive.
* If str, then indicates comma separated list of Excel column letters
and column ranges (e.g. "A:E" or "A,C,E:F"). Ranges are inclusive of
both sides.
* If list of int, then indicates list of column numbers to be parsed.
* If list of string, then indicates list of column names to be parsed.
.. versionadded:: 0.24.0
* If callable, then evaluate each column name against it and parse the
column if the callable returns ``True``.
.. versionadded:: 0.24.0
2、代碼解釋有如下excel表格
# usecols=None也即默認值,默認會解讀所有列
>>> df = pd.read_excel(r'D:/myExcel/1.xlsx', sheet_name='Sheet1',usecols=None)>>> df
name math Chinese
0 bob 23 12
1 millor 32 32
2 jiken 61 89
3 tom 34 94
4 json 83 12
5 dela 96 67
6 rison 90 34
# 當usecols指定[0,1]時則僅parse name列以及math列
>>> df = pd.read_excel(r'D:/myExcel/1.xlsx', sheet_name='Sheet1',usecols=[0,1])>>> df
name math
0 bob 23
1 millor 32
2 jiken 61
3 tom 34
4 json 83
5 dela 96
6 rison 90
# 當指定列名時,則僅parse指定的列名列
>>> df = pd.read_excel(r'D:/myExcel/1.xlsx', sheet_name='Sheet1',usecols=['name','Chinese'])>>> df
name Chinese
0 bob 12
1 millor 32
2 jiken 89
3 tom 94
4 json 12
5 dela 67
6 rison 34
# 當然,usecols還接受一個函數,該函數要求,僅有一個入參,# 要求返回結果必須為#boolen類型,如果為True便會解讀該列# 定義一個函數,如果列名中包含'm'字符,則返回true
>>> def selectcols(col_name):
return 'm' in col_name
>>> df = pd.read_excel(r'D:/myExcel/1.xlsx', sheet_name='Sheet1',usecols=selectcols)>>> df
name math
0 bob 23
1 millor 32
2 jiken 61
3 tom 34
4 json 83
5 dela 96
6 rison 90
>>>
哈哈,以上就是今天的內容,我相信,一定可以幫助到您。建議平常多調用help()函數,雖然是英文的說明,但真的不難。同時,也要多看看pandas文檔,可以關注我的公眾號:python小工具。里面有福利哦。
總結
以上是生活随笔為你收集整理的python如何读取excel的一个sheet_python pandas是如何读取excel表中的sheet的(四)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: python框架大全_常用的Python
- 下一篇: linux wireshark_Wire
