pandas写入excel_Pandas Excel教程:如何读取和写入Excel文件

本教程详细介绍了如何使用Python的Pandas库读取和写入Excel文件。内容涵盖安装Pandas、读取特定列、处理缺失数据、合并多个工作表、读取多个文件以及设置数据类型等。示例丰富,适合Python数据处理初学者。
摘要由CSDN通过智能技术生成

pandas写入excel

In this tutorial we will learn how to work with Excel files and Python. It will provide an overview of how to use Pandas to load and write these spreadsheets to Excel. In the first section, we will go through, with examples, how to read an Excel file, how to read specific columns from a spreadsheet, how to read multiple spreadsheets and combine them to one dataframe, how to read many Excel files, and, finally, how to convert data according to specific datatypes (e.g., using Pandas dtypes). When we have done this, we will continue by learning how to write Excel files; how to name the sheets and how to write to multiple sheets.

在本教程中,我们将学习如何使用Excel文件和Python。 它将概述如何使用熊猫将这些电子表格加载并写入Excel。 在第一部分中,我们将通过示例介绍如何读取Excel文件,如何从电子表格中读取特定列,如何读取多个电子表格并将其合并为一个数据框,如何读取许多Excel文件,以及最后,如何根据特定的数据类型(例如,使用Pandas dtypes)转换数据。 完成此操作后,我们将继续学习如何编写Excel文件。 如何命名工作表以及如何写入多个工作表。

如何安装熊猫 (How to Install Pandas)

Before we continue with this read and write Excel files tutorial there is something we need to do; installing Pandas (and Python, of course, if it’s not installed). We can install Pandas using Pip, given that we have Pip installed, that is. See here how to install pip.

在继续阅读和编写Excel文件教程之前,我们需要做一些事情。 安装Pandas(当然,如果没有安装,则安装Python)。 假设我们已经安装了Pip,则可以使用Pip安装Pandas。 请参阅此处如何安装pip。

# Linux Users
pip install pandas

# Windows Users
python pip install pandas

安装Anaconda Scientific Python发行版 (Installing Anaconda Scientific Python Distribution)

Another great option is to consider is to install the Anaconda Python distribution. This is really an easy and fast way to get started with computer science. No need to worry about installing the packages you need to do computer science separately.

要考虑的另一个不错的选择是安装Anaconda Python发行版 。 这确实是一种轻松快速的计算机科学入门方法。 无需担心需要单独安装计算机科学的软件包。

Both of the above methods are explained in this tutorial.

本教程介绍了以上两种方法。

如何将Excel文件读取到Pandas数据框: (How to Read Excel Files to Pandas Dataframes:)

In this section we are going to learn how to read Excel files and spreadsheets to Pandas dataframe objects. All examples in this Pandas Excel tutorial use local files. Note, that read_excel also can also load Excel files from a URL to a dataframe.  As always when working with Pandas, we have to start by importing the module:

在本节中,我们将学习如何将Excel文件和电子表格读取到Pandas数据框对象。 该Pandas Excel教程中的所有示例均使用本地文件。 请注意,read_excel还可以将Excel文件从URL加载到数据框。 一如往常,在使用Pandas时,我们必须从导入模块开始:

import pandas as pd

Now it’s time to learn how to use Pandas read_excel to read in data from an Excel file. The easiest way to use this method is to pass the file name as a string. If we don’t pass any other parameters, such as sheet name, it will read the first sheet in the index. In the first example we are not going to use any parameters:

现在是时候学习如何使用Pandas read_excel从Excel文件中读取数据了。 使用此方法的最简单方法是将文件名作为字符串传递。 如果我们不传递任何其他参数(例如工作表名称),它将读取索引中的第一张工作表。 在第一个示例中,我们将不使用任何参数:

df = pd.read_excel('MLBPlayerSalaries.xlsx')
df.head()

Here, Pandas read_excel method read the data from the Excel file into a Pandas dataframe object. We then stored this dataframe into a variable called df.

在这里,Pandas的read_excel方法将数据从Excel文件读取到Pandas数据框对象中。 然后,我们将此数据帧存储到名为df的变量中。

When using read_excel Pandas will, by default, assign a numeric index or row label to the dataframe, and as usual when int comes to Python, the index will start with zero. We may have a reason to leave the default index as it is. For instance, if your data doesn’t have a column with unique values that can serve as a better index. In case there is a column that would serve as a better index, we can override the defa

  • 49
    点赞
  • 259
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值