搜尋
首頁後端開發Python教學python中pandas和xlsxwriter讀寫xlsx檔的方法介紹(附程式碼)

這篇文章帶給大家的內容是關於python中pandas和xlsxwriter讀寫xlsx檔的方法介紹(附程式碼),有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

已有xlsx檔案如下:

1. 讀取前n行所有資料

# coding: utf-8
import  pandas  as pd


# 1. 读取前n行所有数据

df = pd.read_excel('school.xlsx')#读取xlsx中第一个sheet
data1 = df.head(7)   # 读取前7行的所有数据,dataFrame结构
data2 = df.values    #list形式,读取表格所有数据
print("获取到所有的值:\n{0}".format(data1)) #格式化输出
print("获取到所有的值:\n{0}".format(data2)) #格式化输出

2. 讀取特定行,特定列

# coding: utf-8
import  pandas  as pd


# 2. 读取特定行,特定列
df = pd.read_excel('school.xlsx')  #读取xlsx中第一个sheet

data1 = df.ix[0].values     #读取第一行所有数据,0表示第一行,不包含表头
data2 = df.ix[1,1]   #读取指定行列位置数据
data3 = df.ix[[1,2]].values   #读取指定多行
data4 = df.ix[:,[0]].values   #读取指定列的所有行
#data4 = df[u'class'].values   #同上
data5 = df.ix[:,[u'class',u'name']].values   #读取指定键值列的所有行

print("数据:\n{0}".format(data1))
print("数据:\n{0}".format(data2))
print("数据:\n{0}".format(data3))
print("数据:\n{0}".format(data4))
print("数据:\n{0}".format(data5))

3. 取得xlsx檔行號,所有列名

# coding: utf-8
import  pandas  as pd


# 3. 获取xlsx文件行号,所有列名称
df = pd.read_excel('school.xlsx')  #读取xlsx中第一个sheet

print("输出行号列表{}".format(df.index.values))   # 获取xlsx文件的所有行号
print("输出列标题{}".format(df.columns.values))   #所有列名称

4. 讀取xlsx資料轉換為字典

# coding: utf-8
import  pandas  as pd


# 4. 读取xlsx数据转换为字典
df = pd.read_excel('school.xlsx')  #读取xlsx中第一个sheet
test_data=[]
for i in df.index.values:#获取行号的索引,并对其进行遍历:
    #根据i来获取每一行指定的数据 并利用to_dict转成字典
    row_data=df.ix[i,['id','name','class','data','stature']].to_dict()
    test_data.append(row_data)
print("最终获取到的数据是:{0}".format(test_data))

5. 寫xlsx檔

#coding: utf-8
import xlsxwriter

# 创建工作簿
file_name = "first_book.xlsx"
workbook = xlsxwriter.Workbook(file_name)

# 创建工作表
worksheet = workbook.add_worksheet('sheet1')

# 写单元格
worksheet.write(0, 0, 'id')
worksheet.write(0,1, 'name')
worksheet.write(0,2, 'class')
worksheet.write(0,3, 'data')

# 写行
worksheet.write_row(1, 0, [1, 2, 3])

# 写列,其中列D需要大写
worksheet.write_column('D2', ['a', 'b', 'c'])

# 关闭工作簿
workbook.close()

寫入的xlsx檔如下:

以上是python中pandas和xlsxwriter讀寫xlsx檔的方法介紹(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:CSDN。如有侵權,請聯絡admin@php.cn刪除
Numpy數組與使用數組模塊創建的數組有何不同?Numpy數組與使用數組模塊創建的數組有何不同?Apr 24, 2025 pm 03:53 PM

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,內存效率段

Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Numpy數組的使用與使用Python中的數組模塊陣列相比如何?Apr 24, 2025 pm 03:49 PM

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

CTYPES模塊與Python中的數組有何關係?CTYPES模塊與Python中的數組有何關係?Apr 24, 2025 pm 03:45 PM

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

在Python的上下文中定義'數組”和'列表”。在Python的上下文中定義'數組”和'列表”。Apr 24, 2025 pm 03:41 PM

Inpython,一個“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“陣列” isamorememory-sepersequeSequeSequeSequeSequeRingequiringElements.1)列表

Python列表是可變還是不變的?那Python陣列呢?Python列表是可變還是不變的?那Python陣列呢?Apr 24, 2025 pm 03:37 PM

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python vs. C:了解關鍵差異Python vs. C:了解關鍵差異Apr 21, 2025 am 12:18 AM

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python vs.C:您的項目選擇哪種語言?Python vs.C:您的項目選擇哪種語言?Apr 21, 2025 am 12:17 AM

選擇Python還是C 取決於項目需求:1)如果需要快速開發、數據處理和原型設計,選擇Python;2)如果需要高性能、低延遲和接近硬件的控制,選擇C 。

達到python目標:每天2小時的力量達到python目標:每天2小時的力量Apr 20, 2025 am 12:21 AM

通過每天投入2小時的Python學習,可以有效提升編程技能。 1.學習新知識:閱讀文檔或觀看教程。 2.實踐:編寫代碼和完成練習。 3.複習:鞏固所學內容。 4.項目實踐:應用所學於實際項目中。這樣的結構化學習計劃能幫助你係統掌握Python並實現職業目標。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。