python3字典写入excel_Python玩转Excel(第2期)~这里只有干货
第1期發(fā)布之后,反響特別好,很快通過審核,不到2小時推薦量2600多,還是挺高的,說明大家對此版塊內(nèi)容還是感興趣的,激發(fā)了我創(chuàng)作的動力。謝謝大家,今天加班再更新一期。
一、如何創(chuàng)建數(shù)據(jù)的‘’糧倉‘’(如何使用pandas向Excel里面寫入數(shù)據(jù))
1.將字典數(shù)據(jù)通過pandas寫入Excel中
import pandas as pd#定義一個字典dic={ 'id':[1,2,3], 'name':['liming','zhangsan','wangwu']}#將字典格式化為DataFrame數(shù)據(jù)data = pd.DataFrame(dic)#將數(shù)據(jù)寫入Excel中data.to_excel('test.xlsx')print(data)********************************* id name0 1 liming1 2 zhangsan2 3 wangwu******************************* #Excel文件內(nèi)容就不在此演示,該數(shù)據(jù)保存在工程當(dāng)前目錄下test.xlsx中2.Pandas將數(shù)據(jù)寫入多個sheet中
import pandas as pd#定義2個字典dic1={ 'id':[1,2,3], 'name':['liming','zhangsan','wangwu']}dic2={ 'id':['001','002','003'], 'name':['liu ying','zhang hao','hua tuo']}#將字典數(shù)據(jù)格式化為DataFrame數(shù)據(jù)df1 = pd.DataFrame(dic1)df2 = pd.DataFrame(dic2)#將數(shù)據(jù)寫入Excel中write = pd.ExcelWriter('test.xlsx')df1.to_excel(write,sheet_name='df1',index=False)df2.to_excel(write,sheet_name='df2',index=False)write.save()write.close()劃重點:這里重點介紹下ExcelWriter()
使用ExcelWriter()可以向同一個excel的不同sheet中寫入對應(yīng)的表格數(shù)據(jù),首先需要創(chuàng)建一個writer對象,傳入的主要參數(shù)為已存在容器表格的路徑及文件名稱:
writer = pd.ExcelWriter(r'D:demo.xlsx')基于已創(chuàng)建的writer對象,可以利用to_excel()方法將不同的數(shù)據(jù)框及其對應(yīng)的sheet名稱寫入該writer對象中,并在全部表格寫入完成之后,使用save()方法來執(zhí)行writer中內(nèi)容向?qū)?yīng)實體excel文件寫入數(shù)據(jù)的過程.
'''創(chuàng)建數(shù)據(jù)框1'''df1 = pd.DataFrame({'V1':np.random.rand(100), 'V2 ':np.random.rand(100), 'V3':np.random.rand(100)})df1.to_excel(writer,sheet_name='sheet1',index=False)'''創(chuàng)建數(shù)據(jù)框2'''df2 = pd.DataFrame({'V1':np.random.rand(100), 'V2 ':np.random.rand(100), 'V3':np.random.rand(100)})df2.to_excel(writer,sheet_name='sheet2',index=False)'''數(shù)據(jù)寫出到excel文件中'''writer.save()write.close()這時之前指定的外部excel文件中便成功存入相應(yīng)的內(nèi)容:
二、做一個可以拼顏值的圖(分組柱圖深度優(yōu)化)
import pandas as pdimport matplotlib.pyplot as plt#讀取數(shù)據(jù)data = pd.read_excel('book2.xlsx')#對數(shù)據(jù)進(jìn)行排序data.sort_values(by=2018,inplace=True,ascending=False)#生成兩組柱狀圖(多組類似)data.plot.bar(x='product',y=[2018,2019],color=['orange','red'])#設(shè)置標(biāo)題plt.title('Product annual sales comparison chart',fontsize=13,fontweight='bold')#設(shè)置x軸plt.xlabel('Product',fontweight='bold')#設(shè)置y軸plt.ylabel('Sales Volume',fontweight='bold')#設(shè)置x軸標(biāo)題斜45度ax=plt.gca()ax.set_xticklabels(data['product'],rotation=45,ha='right')#自動調(diào)整子圖參數(shù),使之填充整個圖像區(qū)域plt.tight_layout()#顯示plt.show()這個效果是不是看起來很高大上,大家可以進(jìn)一步修改參數(shù)進(jìn)行優(yōu)化。因為里面基本都是設(shè)置類的函數(shù),使用起來比較簡單,就不依依介紹了。
三、制作疊加柱狀圖
import pandas as pdimport matplotlib.pyplot as pltdata=pd.read_excel('book3.xlsx'data.plot.bar(x='username',y=['Oct','Nov','dec'],stacked=True)# #自動調(diào)整子圖參數(shù),使之填充整個圖像區(qū)域plt.tight_layout()# # # #顯示plt.show()如果想得到橫向的只需改寫一句代碼
data.plot.barh(x='username',y=['Oct','Nov','dec'],stacked=True)今天的內(nèi)容就分享到這里,Python語言使用起來確實很簡單,很容易上手,但也容易忘,多練、多記才能更好理解和掌握。
人生苦短,我用python。下期見
總結(jié)
以上是生活随笔為你收集整理的python3字典写入excel_Python玩转Excel(第2期)~这里只有干货的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adnroid string拼接_And
- 下一篇: python indices_pytho