【Python12】pandas模块存储Excel文件

1. Pandas模块简介


  • Pandas是python的一个数据分析包,
  • Pandas最初被作为金融数据分析工具而开发出来,Pandas为时间序列分析提供了很好的支持。
  • Pandas是基于NumPy的一种工具。
  • Pandas纳入了大量函数和一些标准的数据模型,提供了高效操作大型数据集所需的工具,提供了大量能使我们快速便捷地处理数据的函数和方法,让Python成为强大而高效的数据分析环境。

安装Pandas:阿里云高速下载

pip install pandas -i https://mirrors.aliyun.com/pypi/simple/

用pandas模块读写各种类型文件

在这里插入图片描述


2. 存储为Excel文件


  • 安装openpyxl模块

pip install openpyxl -i https://mirrors.aliyun.com/pypi/simple/

代码

import pandas
hosts = [
    {
    
    'host': '1.1.1.1', 'hostname': 'test1', 'idc': 'ali'},
    {
    
    'host': '1.1.1.2', 'hostname': 'test2', 'idc': 'ali'},
    {
    
    'host': '1.1.1.3', 'hostname': 'test3', 'idc': 'huawei'},
    {
    
    'host': '1.1.1.4', 'hostname': 'test4', 'idc': 'ali'}
]

# 1. 转换数据类型
df = pandas.DataFrame(hosts)
# 2. 存储到excel文件中
df.to_excel('doc/hosts.xlsx')
print('success!')

效果显示

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46069582/article/details/113731812