用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文件表数据
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对象具有多种实用程序方法,可将表格数据转换为Dict , CSV或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画熊猫