python使用pandas和xlsxwriter讀寫xlsx文件,下面帶給大家相關步驟:
已有xlsx文件如下:
1. 讀取前n行所有資料
## coding: utf-8
import pandas as pd
# 讀取前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
## 讀取特定行,特定列
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)) 相關推薦:《Python影片教學》
3. 取得xlsx檔案行號,所有欄位名稱
# coding: utf-8import pandas as pd# 取得xlsx檔案行號,所有欄位名稱df = pd.read_excel('school.xlsx') #讀取xlsx中第一個sheetprint("輸出行號列表{}".format(df.index.values )) # 取得xlsx檔案的所有行號print("輸出列標題{}".format(df.columns.values)) #所有欄位名稱#4.讀取xlsx資料轉換為字典
# coding: utf-8import pandas as pd# 讀取xlsx資料轉換為字典#df = pd.read_excel('school.xlsx') #讀取xlsx中第一個sheettest_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-8import xlsxwriter##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檔案如下:
以上是怎麼用pd導入xlsx的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Dreamweaver Mac版
視覺化網頁開發工具