系统:windows 7语言版本:anaconda3-4.3.0.1-windows-x86_64编辑器:pycharm-community-2016.3.2pandas:0.19.2
这个系列讲讲Python的科学计算及可视化今天讲讲pandas模块将Df输出到Excel文件中,以及读取Excel中数据Part 1:场景介绍

Part 2:代码

<pre class="brush:php;toolbar:false;">import pandas as pdimport osdict_1 = {"time": ["2019-11-02", "2019-11-03", "2019-11-04", "2019-11-05", "2019-12-02", "2019-12-03", "2019-12-04", "2019-12-05"], "pos": ["A", "A", "B", "B", "C", "C", "C", "D"], "value1": [10, 20, 30, 40, 50, 60, 70, 80]}df_1 = pd.DataFrame(dict_1, columns=["time", "pos", "value1"])print("原数据", "\n", df_1, "\n")# 输出到Excelcurrent_address = os.path.dirname(os.path.abspath(__file__))excel_name = "df.xlsx"excel_address = os.path.join(current_address, excel_name)df_1.to_excel(excel_address)excel_name_2 = "df_2.xlsx"excel_address_2 = os.path.join(current_address, excel_name_2)df_2 = df_1.head(3)df_2.to_excel(excel_address_2)# 读Excel数据df_3 = pd.read_excel(excel_address)print(df_3)excel_name_4 = "test.xlsx"excel_address_4 = os.path.join(current_address, excel_name_4)df_4 = pd.read_excel(excel_address_4, sheetname="ceshi", header=0)print(df_4)代码截图

excel_address
立即学习“Python免费学习笔记(深入)”;

excel_address_2

test.xlsx

代码运行命令窗口输出

Part 3:部分代码解读

df_1.to_excel(excel_address)
df_2 = df_1.head(3)
df_3 = pd.read_excel(excel_address)
sheetname="ceshi"
df_4 = pd.read_excel(excel_address_4, sheetname="ceshi", header=0)
read_excel参数

长按图片识别二维码,关注本公众号Python 优雅 帅气
以上就是Python-科学计算-pandas-15-df输出Excel和解析Excel的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号