??接上文。
數(shù)據(jù)分析時(shí)候,需要將數(shù)據(jù)進(jìn)行加載和存儲(chǔ)。
今天來(lái)講講pandas怎么和excel互動(dòng)!
首先需要安裝第三方模塊
pip install openpyxl
pip install xlrd
這樣就ok了,和安裝什么其他的python模塊一樣。
把pd數(shù)據(jù)寫(xiě)入excel
import pandas as pd
writer = pd.ExcelWriter('/tmp/output.xlsx')
#創(chuàng)建一個(gè)writer對(duì)象
df_test = pd.DataFrame(np.arange(9).reshape(3,3), index=["Mon","Tue","Wed"], columns=['store1', 'store2', 'store3'])
#創(chuàng)建測(cè)試用的df數(shù)據(jù)
df_test.to_excel(writer)
#把df內(nèi)容寫(xiě)入文件
writer.save()
#記得最后一步,沒(méi)有就不會(huì)保存。
去/tmp目錄下你可以查看新生成的文件

output.xlsx
從excel文件中讀取數(shù)據(jù)
file_path = '/tmp/output.xlsx'
df_test = pd.read_excel(file_path, index_col=0)
df_test
輸出如下:

df_test