日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Python使用pandas導(dǎo)入xlsx格式的excel文件內(nèi)容操作代碼_python

作者:smart_cat ? 更新時(shí)間: 2023-01-29 編程語(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)

20221222175733

電腦中的文件路徑默認(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)

效果如下:

20221222180651

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)

20221222180855

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)

20221222181152

6. 指定導(dǎo)入列

有的時(shí)候本地文件的列數(shù)太多,而我們又不需要那么多列時(shí),我們就可以通過(guò)設(shè)定usecols參數(shù)來(lái)指定要導(dǎo)入的列。

20221222181410

從參數(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)

20221222182310

df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, usecols=['姓名','性別'])
print(df)

20221222182659

df = pd.read_excel(r'G:\test.xlsx', sheet_name=0, usecols=('姓名','年齡'))
print(df)

20221222182822

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)

20221222183203

8. 更多的參數(shù)

請(qǐng)參考pandas官方文檔。

原文鏈接:https://blog.csdn.net/hubing_hust/article/details/128412197

欄目分類
最近更新