Trong phần này, tôi sẽ trình bày cách sử dụng thư viện Pandas với Excel. Chúng tôi sẽ sử dụng các thao tác bảng excel cơ bản như tạo trang tính mới, thêm dữ liệu hàng loạt, nối thêm dữ liệu, đọc dữ liệu, định dạng dữ liệu và thêm biểu đồ
Cài đặt
Thực hiện các lệnh sau để cài đặt các thư viện Pandas, xlsxwriter, xlrd và openpyxl
pip install pandaspip install xlsxwriterpip install xlrdpip install openpyxl
Tạo một bảng tính Excel
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]
Mã này sẽ tạo một tệp
import pandas as pd
# dataframe Name and Age columns
df = pd.DataFrame[{'Name': ['A', 'B', 'C', 'D'],
'Age': [10, 0, 30, 50]}]
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel[writer, sheet_name='Sheet1', index=False]
# Close the Pandas Excel writer and output the Excel file.
writer.close[]
0 mới với trang tính mặc định có tên là import pandas as pd
# dataframe Name and Age columns
df = pd.DataFrame[{'Name': ['A', 'B', 'C', 'D'],
'Age': [10, 0, 30, 50]}]
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']
# Convert the dataframe to an XlsxWriter Excel object.
df.to_excel[writer, sheet_name='Sheet1', index=False]
# Close the Pandas Excel writer and output the Excel file.
writer.close[]
1tờ2
Cài đặt xlrd
gấu trúc. . read_excel một. ] sử dụng thư viện có tên xlrd trong nội bộ
xlrd là một thư viện để đọc [nhập] tệp Excel [. xlsx,. xls] bằng Python
bài viết liên quan. Cách dùng xlrd, xlwt đọc ghi file Excel bằng Python
Nếu bạn gọi gấu trúc. read_excel s[] trong môi trường chưa cài đặt xlrd, bạn sẽ nhận được thông báo lỗi tương tự như sau
Nhập Lỗi. Cài đặt xlrd >= 0. 9. 0 để được hỗ trợ Excel
xlrd có thể được cài đặt bằng pip. [pip3 tùy thuộc vào môi trường]
1
$ pip install xlrd
đọc excel
Chỉ định đường dẫn hoặc URL của tệp Excel trong đối số đầu tiên.
Nếu có nhiều trang tính, gấu trúc chỉ sử dụng trang tính đầu tiên.
Nó đọc là DataFrame.
1
2
3
4
5
import pandas as pd
df = pd.read_excel['sample.xlsx']
print[df]
Đoạn code trên xuất ra nội dung sheet excel
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]0
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]1
Nhận trang tính
Bạn có thể chỉ định trang tính để đọc với đối số sheet_name
Chỉ định theo số [bắt đầu từ 0]
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]2____13
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]0
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]5
Chỉ định theo tên trang tính
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]2____71
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]0
import pandas as pdwriter = pd.ExcelWriter['demo.xlsx', engine='xlsxwriter']writer.close[]5
Tải nhiều tờ
Cũng có thể chỉ định một danh sách trong đối số sheet_name. Nó là OK ngay cả khi nó là một số 0 bắt đầu hoặc tên trang tính
Số hoặc tên trang tính được chỉ định là khóa chính và gấu trúc dữ liệu. DataFrame được đọc dưới dạng từ điển được đặt hàng OrderedDict với giá trị giá trị