用python画熊猫_熊猫read_excel()–用Python读取Excel文件

这篇博客介绍了如何利用pandas的read_excel()函数在Python中读取Excel文件。内容包括读取指定工作表、获取列标题列表、打印列数据、选择性读取列、处理无标题行的Excel文件,以及将数据转换为Dict、CSV和JSON格式。
摘要由CSDN通过智能技术生成

用python画熊猫

We can use the pandas module read_excel() function to read the excel file data into a DataFrame object.

我们可以使用pandas模块的 read_excel()函数将Excel文件数据读入DataFrame对象。

If you look at an excel sheet, it’s a two-dimensional table. The DataFrame object also represents a two-dimensional tabular data structure.

如果您查看一个Excel工作表,它是一个二维表。 DataFrame对象还表示二维表格数据结构。

1.熊猫read_excel()示例 (1. Pandas read_excel() Example)

Let’s say we have an excel file with two sheets – Employees and Cars. The top row contains the header of the table.

假设我们有一个包含两张纸的excel文件-员工和汽车。 第一行包含表的标题。

Excel File Sheets Data

Excel File Sheets Data

Excel文件表数据

Here is the example to read the “Employees” sheet data and printing it.

这是读取并打印“雇员”工作表数据的示例。

import pandas

excel_data_df = pandas.read_excel('records.xlsx', sheet_name='Employees')

# print whole sheet data
print(excel_data_df)

Output:

输出:

EmpID    EmpName EmpRole
0      1     Pankaj     CEO
1      2  David Lee  Editor
2      3   Lisa Ray  Author
  • The first parameter is the name of the excel file.

    第一个参数是excel文件的名称。
  • The sheet_name parameter defines the sheet to be read from the excel file.

    sheet_name参数定义要从excel文件读取的图纸。
  • When we print the DataFrame object, the output is a two-dimensional table. It looks similar to an excel sheet records.

    当我们打印DataFrame对象时,输出是一个二维表。 它看起来类似于Excel工作表记录。

2. Excel工作表的列标题列表 (2. List of Columns Headers of the Excel Sheet)

We can get the list of column headers using the columns property of the dataframe object.

我们可以使用dataframe对象的columns属性获取列标题列表。

print(excel_data_df.columns.ravel())

Output:

输出:

['EmpID' 'EmpName' 'EmpRole']

3.打印列数据 (3. Printing a Column Data)

We can get the column data and convert it into a list of values.

我们可以获取列数据并将其转换为值列表。

print(excel_data_df['EmpName'].tolist())

Output:

输出:

['Pankaj', 'David Lee', 'Lisa Ray']

4.熊猫read_excel()usecols示例 (4. Pandas read_excel() usecols example)

We can specify the column names to be read from the excel file. It’s useful when you are interested in only a few of the columns of the excel sheet.

我们可以指定要从excel文件读取的列名。 当您仅对excel工作表的几列感兴趣时,此功能很有用。

import pandas

excel_data_df = pandas.read_excel('records.xlsx', sheet_name='Cars', usecols=['Car Name', 'Car Price'])
print(excel_data_df)

Output:

输出:

Car Name      Car Price
0      Honda City     20,000 USD
1  Bugatti Chiron  3 Million USD
2     Ferrari 458   2,30,000 USD

5.读取没有标题行的Excel文件 (5. Reading Excel File without Header Row)

If the excel sheet doesn’t have any header row, pass the header parameter value as None.

如果excel工作表没有任何标题行,请将标题参数值传递为None。

excel_data_df = pandas.read_excel('records.xlsx', sheet_name='Numbers', header=None)

If you pass the header value as an integer, let’s say 3. Then the third row will be treated as the header row and the values will be read from the next row onwards. Any data before the header row will be discarded.

如果将标头值作为整数传递,则假设3。然后,将第三行视为标头行,并且将从下一行开始读取值。 标头行之前的所有数据都将被丢弃。

6. Excel工作表用于Dict,CSV和JSON (6. Excel Sheet to Dict, CSV and JSON)

The DataFrame object has various utility methods to convert the tabular data into Dict, CSV, or JSON format.

DataFrame对象具有多种实用程序方法,可将表格数据转换为DictCSV或JSON格式。

excel_data_df = pandas.read_excel('records.xlsx', sheet_name='Cars', usecols=['Car Name', 'Car Price'])

print('Excel Sheet to Dict:', excel_data_df.to_dict(orient='record'))
print('Excel Sheet to JSON:', excel_data_df.to_json(orient='records'))
print('Excel Sheet to CSV:\n', excel_data_df.to_csv(index=False))

Output:

输出:

Excel Sheet to Dict: [{'Car Name': 'Honda City', 'Car Price': '20,000 USD'}, {'Car Name': 'Bugatti Chiron', 'Car Price': '3 Million USD'}, {'Car Name': 'Ferrari 458', 'Car Price': '2,30,000 USD'}]
Excel Sheet to JSON: [{"Car Name":"Honda City","Car Price":"20,000 USD"},{"Car Name":"Bugatti Chiron","Car Price":"3 Million USD"},{"Car Name":"Ferrari 458","Car Price":"2,30,000 USD"}]
Excel Sheet to CSV:
 Car Name,Car Price
Honda City,"20,000 USD"
Bugatti Chiron,3 Million USD
Ferrari 458,"2,30,000 USD"

7.参考 (7. References)

翻译自: https://www.journaldev.com/33306/pandas-read_excel-reading-excel-file-in-python

用python画熊猫

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值