網站首頁 編程語言 正文
Python使用pandas導入xlsx格式的excel文件內容
1. 基本導入
在 Python中使用pandas導入.xlsx文件的方法是read_excel()。
# coding=utf-8
import pandas as pd
df = pd.read_excel(r'G:\test.xlsx')
print(df)
電腦中的文件路徑默認使用\,這個時候需要在路徑前面加一個r(轉義符)避免路徑里面的\被轉義。也可以不加 r,但是需要把路徑里面的所有\轉換成/,這個規則在導入其他格式文件時也是一樣的,我們一般選擇在路徑前面加r
2. 列標題與數據對齊
因為我們的表格中有中文,中文占用的字符和英文、數字占用的字符不一樣,因此需要調用pd.set_option()使表格對齊顯示。如果你是使用 Jupyter 來運行代碼的,Jupyter 會自動渲染出一個表格,則無需這個設置。
import pandas as pd
#處理數據的列標題與數據無法對齊的情況
pd.set_option('display.unicode.ambiguous_as_wide', True)
#無法對齊主要是因為列標題是中文
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx')
print(df)
效果如下:
3. 指定導入某個sheet
通過sheet_name參數可以指定要導入哪個sheet的內容。注意這里的名字是區分大小寫的。
import pandas as pd
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx', sheet_name='Sheet1')
print(df)
除了可以指定具體的sheet名字,還可以傳入sheet的index下標,從0開始計數。例如:
# coding=utf-8
import pandas as pd
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0)
print(df)
如果不指定sheet_name參數,那么默認導入的都是第一個sheet的內容。
4. 指定行索引
在本地文件導入DataFrame時,行索引使用的從0開始的默認索引,可以通過設置index_col參數來設置。
# coding=utf-8
import pandas as pd
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, index_col=0)
print(df)
5. 指定列索引
將本地文件導入DataFrame時,默認使用源數據表的第一行作為列索引,也可以通過設置header參數來設置列索引。 header參數值默認為0,即用第一行作為列索引;也可以是其他行,只需要傳入具體的那一行即可;也可以使用默認從0開始的數作為列索引。
使用默認從0開始的數作為列索引示意:
# coding=utf-8
import pandas as pd
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, header=None)
print(df)
6. 指定導入列
有的時候本地文件的列數太多,而我們又不需要那么多列時,我們就可以通過設定usecols參數來指定要導入的列。
從參數的形式來看,可以通過以下幾種形式來指定:
- 通過列表指定,列表中是列的下標,從0開始計數。
- 通過列表指定,列表中是列的名字
- 通過元組指定, 元組中是列的名字
示例如下:
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, usecols=[0,1])
print(df)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, usecols=['姓名','性別'])
print(df)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, usecols=('姓名','年齡'))
print(df)
7. 指定導入的行數
如果文件很大,我們不想導入全部的行,只需要導入前面若干行進行分析即可,那么可以通過nrows參數來指定導入多少行數據
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, nrows=2)
print(df)
8. 更多的參數
請參考pandas官方文檔。
原文鏈接:https://blog.csdn.net/hubing_hust/article/details/128412197
相關推薦
- 2022-04-01 關于python中if __name=‘__main__‘的理解
- 2022-10-05 Numpy中Meshgrid函數基本用法及2種應用場景_python
- 2022-03-23 shell腳本設置防止暴力破解ssh_Linux
- 2022-10-15 Windows10搭建FTP服務器詳細教程_FTP服務器
- 2022-06-06 自定義overflow產生的滾動條樣式設置
- 2023-02-02 C語言求素數的幾種方式總結_C 語言
- 2023-06-21 C#高級靜態語言效率利器之泛型詳解_C#教程
- 2022-10-19 R語言安裝以及手動安裝devtools的詳細圖文教程_R語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支