【文件处理】——Python pandas 写入数据到excel中
生活随笔
收集整理的這篇文章主要介紹了
【文件处理】——Python pandas 写入数据到excel中
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
1、創(chuàng)建一個(gè)新的excel表格
2、 獲取寫入excel的數(shù)據(jù)data
3、將data類型轉(zhuǎn)換為pandas接受的類型
4、寫入到excel中
5、保存excel
最終結(jié)果
?
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/11/9 23:18 # @Author : @linlianqin # @Site : # @File : writeDataIntoExcel.py # @Software: PyCharm # @description:import pandas as pddef writeDataIntoExcel(xlsPath: str, data: dict):writer = pd.ExcelWriter(xlsPath)sheetNames = data.keys() # 獲取所有sheet的名稱# sheets是要寫入的excel工作簿名稱列表data = pd.DataFrame(data)for sheetName in sheetNames:data.to_excel(writer, sheet_name=sheetName)# 保存writer中的數(shù)據(jù)至excel# 如果省略該語(yǔ)句,則數(shù)據(jù)不會(huì)寫入到上邊創(chuàng)建的excel文件中writer.save()if __name__ == '__main__':data = {"name":["lily","ailcie"],"cost":[100,20]}xlspath = "3.xls"writeDataIntoExcel(xlspath,data)1、創(chuàng)建一個(gè)新的excel表格
writer = pd.ExcelWriter(xlsPath)
2、 獲取寫入excel的數(shù)據(jù)data
data的形式是字典的形式,
data = {字段名1:[字段1內(nèi)容1,字段1內(nèi)容2,...,字段1內(nèi)容n],...,字段名n:[字段n內(nèi)容1,字段n內(nèi)容2,...,字段n內(nèi)容n]}
注:字段指的是excel中每一列的列名稱
data = {"name":["lily","ailcie"],"cost":[100,20]}
3、將data類型轉(zhuǎn)換為pandas接受的類型
data = pd.DataFrame(data)
4、寫入到excel中
data.to_excel(writer, sheet_name=sheetName)5、保存excel
writer.save()最終結(jié)果
?
總結(jié)
以上是生活随笔為你收集整理的【文件处理】——Python pandas 写入数据到excel中的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb重启是什么意思
- 下一篇: 批量找注入 python3+sqlmap