網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容
1. 基本導(dǎo)入
在 Python中使用pandas導(dǎo)入.xlsx文件的方法是read_excel()。
# coding=utf-8
import pandas as pd
df = pd.read_excel(r'G:\test.xlsx')
print(df)
電腦中的文件路徑默認(rèn)使用\,這個(gè)時(shí)候需要在路徑前面加一個(gè)r(轉(zhuǎn)義符)避免路徑里面的\被轉(zhuǎn)義。也可以不加 r,但是需要把路徑里面的所有\(zhòng)轉(zhuǎn)換成/,這個(gè)規(guī)則在導(dǎo)入其他格式文件時(shí)也是一樣的,我們一般選擇在路徑前面加r
2. 列標(biāo)題與數(shù)據(jù)對(duì)齊
因?yàn)槲覀兊谋砀裰杏兄形模形恼加玫淖址陀⑽摹?shù)字占用的字符不一樣,因此需要調(diào)用pd.set_option()使表格對(duì)齊顯示。如果你是使用 Jupyter 來(lái)運(yùn)行代碼的,Jupyter 會(huì)自動(dòng)渲染出一個(gè)表格,則無(wú)需這個(gè)設(shè)置。
import pandas as pd
#處理數(shù)據(jù)的列標(biāo)題與數(shù)據(jù)無(wú)法對(duì)齊的情況
pd.set_option('display.unicode.ambiguous_as_wide', True)
#無(wú)法對(duì)齊主要是因?yàn)榱袠?biāo)題是中文
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_excel(r'G:\test.xlsx')
print(df)
效果如下:
3. 指定導(dǎo)入某個(gè)sheet
通過(guò)sheet_name參數(shù)可以指定要導(dǎo)入哪個(gè)sheet的內(nèi)容。注意這里的名字是區(qū)分大小寫(xiě)的。
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下標(biāo),從0開(kāi)始計(jì)數(shù)。例如:
# 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參數(shù),那么默認(rèn)導(dǎo)入的都是第一個(gè)sheet的內(nèi)容。
4. 指定行索引
在本地文件導(dǎo)入DataFrame時(shí),行索引使用的從0開(kāi)始的默認(rèn)索引,可以通過(guò)設(shè)置index_col參數(shù)來(lái)設(shè)置。
# 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. 指定列索引
將本地文件導(dǎo)入DataFrame時(shí),默認(rèn)使用源數(shù)據(jù)表的第一行作為列索引,也可以通過(guò)設(shè)置header參數(shù)來(lái)設(shè)置列索引。 header參數(shù)值默認(rèn)為0,即用第一行作為列索引;也可以是其他行,只需要傳入具體的那一行即可;也可以使用默認(rèn)從0開(kāi)始的數(shù)作為列索引。
使用默認(rèn)從0開(kāi)始的數(shù)作為列索引示意:
# 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. 指定導(dǎo)入列
有的時(shí)候本地文件的列數(shù)太多,而我們又不需要那么多列時(shí),我們就可以通過(guò)設(shè)定usecols參數(shù)來(lái)指定要導(dǎo)入的列。
從參數(shù)的形式來(lái)看,可以通過(guò)以下幾種形式來(lái)指定:
- 通過(guò)列表指定,列表中是列的下標(biāo),從0開(kāi)始計(jì)數(shù)。
- 通過(guò)列表指定,列表中是列的名字
- 通過(guò)元組指定, 元組中是列的名字
示例如下:
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. 指定導(dǎo)入的行數(shù)
如果文件很大,我們不想導(dǎo)入全部的行,只需要導(dǎo)入前面若干行進(jìn)行分析即可,那么可以通過(guò)nrows參數(shù)來(lái)指定導(dǎo)入多少行數(shù)據(jù)
df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, nrows=2)
print(df)
8. 更多的參數(shù)
請(qǐng)參考pandas官方文檔。
原文鏈接:https://blog.csdn.net/hubing_hust/article/details/128412197
相關(guān)推薦
- 2023-07-07 CreateObject創(chuàng)建vbs對(duì)象時(shí)不支持中文而報(bào)錯(cuò)
- 2022-05-05 Python學(xué)習(xí)之字符串常用方法總結(jié)_python
- 2022-03-13 C語(yǔ)言打印各種圖案實(shí)例代碼_C 語(yǔ)言
- 2022-11-17 React狀態(tài)管理Redux的使用介紹詳解_React
- 2022-09-30 Docker容器harbor私有倉(cāng)庫(kù)部署和管理_docker
- 2022-06-20 Python使用PyAudio制作錄音工具的實(shí)現(xiàn)代碼_python
- 2022-01-15 解決npm install 報(bào)錯(cuò) npm ERR! code 128 npm ERR! comman
- 2022-04-12 C#實(shí)現(xiàn)六大設(shè)計(jì)原則之里氏替換原則_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支