将Excel电子表格加载为pandas DataFrame
Pandas是一个非常强大和可扩展的数据分析工具。它支持多种文件格式,因为我们可能得到任何格式的数据。Pandas也支持excel文件格式。
我们首先需要导入Pandas并加载excel文件,然后将excel文件中的表单解析为Pandas数据框。
import pandas as pd
# Import the excel file and call it xls_file
excel_file = pd.ExcelFile('pandasEx.xlsx')
# View the excel_file's sheet names
print(excel_file.sheet_names)
# Load the excel_file's Sheet1 as a dataframe
df = excel_file.parse('Sheet1')
print(df)
输出:
人们还可以使用read_excel()方法的 “usecols “参数来读取特定的列。
# import pandas lib as pd
import pandas as pd
require_cols = [0, 3]
# only read specific columns from an excel file
required_df = pd.read_excel('SampleWork2.xlsx', usecols = require_cols)
print(required_df)
输出:
Name Percentage
0 Ankit 95
1 Rahul 90
2 Shaurya 85
3 Aishwarya 80
4 Priyanka 75