python使用pandas读取json文件并进行刷选导出xlsx文件的方法示例
更新时间:2023年06月01日 09:19:50 作者:他强任他强03
这篇文章主要介绍了python使用pandas读取json文件并进行刷选导出xlsx文件的方法,结合实例形式分析了python调用pandas模块针对json数据操作的相关使用技巧,需要的朋友可以参考下
pandas读取json文件并进行刷选导出xlsx文件
原始json数据
import pandas as pd
import pprint
# 读取json文件
df_tv_shows = pd.read_json("datas/tv_shows.json")
# 读取json文件中shows数组下所有数据
first_obj = df_tv_shows.loc[:, "shows"]
# 将shows数组数据读出,其中episodes是shows下的数组
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())
# 将刷选出三组数据导出为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数据格式如下:
最后导出的xlxs文件:
python在办公自动化处理方面有着独到的优势,代码相对简洁高效,但是要注意2与3版本的区别。
相关文章
Python range函数生成一系列连续整数的内部机制解析
这篇文章主要为大家介绍了Python range函数生成一系列连续整数的内部机制解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-12-12
python+selenium+Chrome options参数的使用
这篇文章主要介绍了python+selenium+Chrome options参数的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-03-03


最新评论