python使用pandas讀取json文件并進行刷選導(dǎo)出xlsx文件的方法示例
更新時間:2023年06月01日 09:19:50 作者:他強任他強03
這篇文章主要介紹了python使用pandas讀取json文件并進行刷選導(dǎo)出xlsx文件的方法,結(jié)合實例形式分析了python調(diào)用pandas模塊針對json數(shù)據(jù)操作的相關(guān)使用技巧,需要的朋友可以參考下
pandas讀取json文件并進行刷選導(dǎo)出xlsx文件
原始json數(shù)據(jù)
import pandas as pd import pprint # 讀取json文件 df_tv_shows = pd.read_json("datas/tv_shows.json") # 讀取json文件中shows數(shù)組下所有數(shù)據(jù) first_obj = df_tv_shows.loc[:, "shows"] # 將shows數(shù)組數(shù)據(jù)讀出,其中episodes是shows下的數(shù)組 df_tmp = pd.json_normalize(data=first_obj, record_path="episodes", meta=["show", "runtime","network"]) pprint.pprint(df_tmp) # 刷選出show=The X-Files df_tmp1=df_tmp[df_tmp["show"]=="The X-Files"] print(df_tmp1.head()) print(df_tmp1.info()) # 刷選出show=Lost df_tmp2=df_tmp[df_tmp["show"]=="Lost"] print(df_tmp2.head()) print(df_tmp2.info()) # 刷選出show=Buffy the Vampire Slayer df_tmp3=df_tmp[df_tmp["show"]=="Buffy the Vampire Slayer"] print(df_tmp3.head()) print(df_tmp3.info()) # 將刷選出三組數(shù)據(jù)導(dǎo)出為xlsx文件 #xlsx文件名稱 excel_file = pd.ExcelWriter("episodes.xlsx") df_tmp1.to_excel(excel_writer=excel_file, sheet_name = "xfiles", index = False) df_tmp2.to_excel(excel_writer=excel_file, sheet_name = "lost", index = False) df_tmp3.to_excel(excel_writer=excel_file, sheet_name = "vampire", index = False) excel_file.save()
df_tmp數(shù)據(jù)格式如下:
最后導(dǎo)出的xlxs文件:
python在辦公自動化處理方面有著獨到的優(yōu)勢,代碼相對簡潔高效,但是要注意2與3版本的區(qū)別。
相關(guān)文章
對Python 兩大環(huán)境管理神器 pyenv 和 virtualenv詳解
今天小編就為大家分享一篇對Python 兩大環(huán)境管理神器 pyenv 和 virtualenv詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python實現(xiàn)銀行賬戶資金交易管理系統(tǒng)
這篇文章主要介紹了Python銀行賬戶資金交易管理系統(tǒng),本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01