Tìm hiểu cách nhập tệp Excel [có. xlsx] bằng cách sử dụng gấu trúc python
Pandas là gói thao tác dữ liệu phổ biến nhất trong Python và DataFrames là loại dữ liệu Pandas để lưu trữ dữ liệu 2D dạng bảng. Đọc dữ liệu từ tệp excel hoặc tệp CSV và ghi dữ liệu vào tệp Excel hoặc tệp CSV bằng Python Pandas là kỹ năng cần thiết cho bất kỳ nhà phân tích hoặc nhà khoa học dữ liệu nào
Mục lục
1. Pandas read_excel[] Cú pháp
Cú pháp của hàm DataFrame to_excel[] và một số tham số quan trọng là
pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
Sr. NoParameters Description1iođường dẫn tệp từ nơi bạn muốn đọc dữ liệu. Đây có thể là đường dẫn URL hoặc, có thể là đường dẫn tệp hệ thống cục bộ. Lược đồ URL hợp lệ bao gồm http, ftp, s3 và tệp. 2sheet_name. str, int, list hoặc Không có, mặc định 0
Các trường hợp khả dụng.
~Mặc định là
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
1. Trang tính đầu tiên dưới dạng Khung dữ liệu~Sử dụng
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
2. Để đọc trang thứ 2 dưới dạng Khung dữ liệu~Sử dụng Tên trang cụ thể.
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
3 để tải trang tính có tên “Sheet1”~Tải nhiều trang tính bằng dict.
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
4 sẽ tải trang đầu tiên, thứ ba và trang tính có tên “MySheet” dưới dạng từ điển của DataFrame~None. Load All sheet3header
mặc định là 0. Vượt qua Header = 1 để coi dòng thứ hai của tập dữ liệu là tiêu đề. Sử dụng Không có nếu không có tiêu đề. 4usecols
~Mặc định là
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
5, sau đó phân tích cú pháp tất cả các cột. ~Nếu
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
6, thì hãy cung cấp danh sách các cột Excel [“A, B, D, E”] hoặc phạm vi cột Excel [e. g. "MỘT. F” hoặc “A, B, E. f”]. Phạm vi bao gồm cả hai bên. ~Nếu
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
7, cho biết danh sách các số cột sẽ được phân tích cú pháp e. g. [1,2,5]. ~Nếu
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
8, hãy cung cấp danh sách tên cột sẽ được phân tích cú pháp e. g. [“A, B, D, E”]. 5 mũi tên. Mặc định là
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
5Số hàng để phân tích [cung cấp int]. Python Pandas read_excel[] Cú pháp
Để biết danh sách đầy đủ các tham số read_excel, hãy tham khảo tài liệu chính thức
2. Nhập tệp Excel bằng Python Pandas
Hãy xem lại một ví dụ đầy đủ
- Tạo DataFrame từ đầu và lưu dưới dạng Excel
- Nhập [hoặc tải] DataFrame từ tệp Excel đã lưu ở trên
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
Ta có dữ liệu sau về sinh viên
first_nameđộ tuổi0SamPhD251ZivaMBA292Kia193RobinMS21Đọc tệp Excel vào Pandas DataFrame [Đã giải thích]
Bây giờ, hãy xem các bước để nhập tệp Excel vào DataFrame
Bước 1. Nhập đường dẫn và tên tệp nơi lưu trữ tệp Excel. Có thể là đường dẫn tệp hệ thống cục bộ hoặc đường dẫn URL
Ví dụ,
pd. read_excel[r‘ D. \Python\Tutorial\Ví dụ1. csv ‘]
Lưu ý rằng đường dẫn được đánh dấu bằng 3 màu khác nhau
- Phần màu xanh lam biểu thị đường dẫn lưu tệp Excel.
- Phần màu xanh lá cây là tên của tệp bạn muốn nhập.
- Phần màu tím biểu thị loại tệp hoặc phần mở rộng tệp Excel. Sử dụng '. xlsx’ trong trường hợp là tệp Excel.
Sửa đổi mã Python ở trên để phản ánh đường dẫn lưu trữ tệp Excel trên máy tính của bạn
Ghi chú. Bạn có thể lưu hoặc đọc tệp Excel mà không cần cung cấp rõ ràng đường dẫn tệp [ phần màu xanh dương ] bằng cách đặt tệp vào thư mục làm việc hiện tại. Để tìm đường dẫn thư mục hiện tại, hãy sử dụng mã bên dưới.
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
1import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
2Tìm hiểu cách đọc nhiều tệp trong một thư mục [thư mục] tại đây
Bước 2. Nhập mã sau và thực hiện các thay đổi cần thiết đối với đường dẫn của bạn để đọc tệp Excel
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
3Ảnh chụp biểu diễn dữ liệu trong tệp Excel
Ở bên trái của hình ảnh Tệp Excel được mở trong Microsoft Excel. Ở phía bên phải, cùng một tệp Excel được mở trong Jupyter Notebook bằng pandas read_excel
3. Pandas read_excel Ví dụ về các tham số quan trọng
3. 1 Nhập trang tính Excel cụ thể bằng Python Pandas
Có thể có nhiều Sheet trong một file Excel. Pandas cung cấp nhiều phương thức khác nhau để nhập một hoặc nhiều trang tính excel trong tham số
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
10- Mặc định là
1. Đọc trang tính đầu tiên trong Excel dưới dạng DataFrameimport pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Sử dụng
2. Để đọc trang thứ 2 dưới dạng DataFrameimport pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Sử dụng tên trang tính cụ thể.
3 để tải trang tính có tên “Sheet1”import pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Tải nhiều trang tính bằng dict.
4 sẽ tải trang đầu tiên, thứ ba và trang tính có tên “MySheet” dưới dạng từ điển của DataFrameimport pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Không có. Tải tất cả các tờ
1. Nhập trang tính Excel bằng số nguyên
Theo mặc định,
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
15 nhập trang tính đầu tiên trong Excel dưới dạng DataFrame. Để nhập Bảng tính Excel thứ hai tôi. e. "Thông tin về lương" trong trường hợp của chúng tôi dưới dạng Pandas DataFrame sử dụng import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
16import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
1first_name salary0Sam1200001Ziva800002Kia1100003Robin1500002. Nhập trang tính Excel cụ thể bằng Tên trang tính
Để nhập Bảng tính Excel cụ thể i. e. "Thông tin cá nhân" dưới dạng Khung dữ liệu Pandas bằng cách sử dụng
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
17import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
3first_nameđộ tuổi0SamPhD251ZivaMBA292KiaNaN193RobinMS213. Nhập nhiều trang tính Excel vào Pandas DataFrame
Nhiều Trang tính Excel có thể được đọc vào Pandas DataFrame bằng cách chuyển danh sách trong tham số
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
10 e. g. [0, “Thông tin lương”] sẽ tải trang tính đầu tiên và trang tính có tên “Thông tin lương” dưới dạng từ điển của DataFrameimport pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
5Bây giờ để lưu trữ các trang tính khác nhau vào các Khung dữ liệu khác nhau, hãy sử dụng Giá trị khóa từ điển
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
63. 2 Chỉ nhập n Hàng của Trang tính Excel bằng Pandas
Đôi khi tệp Excel khá lớn hoặc hệ thống của chúng tôi có giới hạn về bộ nhớ. Trong trường hợp này, chúng tôi chỉ có thể nhập n hàng trên cùng của Trang tính Excel bằng tham số Pandas read_excel
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
19. Ví dụ: để chỉ nhập 2 hàng trên cùng, hãy sử dụng import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
20import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
9first_nameđộ tuổi0SamPhD251ZivaMBA293. 3 Nhập các cột cụ thể của Bảng tính Excel
Có thể có hàng trăm cột trong bảng excel, nhưng khi nhập chúng ta chỉ cần vài cột. Trong trường hợp này, chúng ta có thể truyền tham số
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
21. Dưới đây là các cách khác nhau để sử dụng tham số import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
21- Mặc định là
5, phân tích cú pháp tất cả các cộtimport pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Nếu
6, thì hãy cung cấp danh sách các cột Excel [“A, B, D, E”] hoặc phạm vi cột Excel [e. g. "MỘT. F” hoặc “A, B, E. f”]. Phạm vi bao gồm cả hai bênimport pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Nếu
7, cho biết danh sách các số cột sẽ được phân tích cú pháp e. g. [0,2,5]import pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
- Nếu
8, hãy cung cấp danh sách tên cột sẽ được phân tích cú pháp e. g. [“A, B, D, E”]import pandas as pd # Create a dataframe raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'], 'degree': ['PhD','MBA','','MS'], 'age': [25, 29, 19, 21]} df = pd.DataFrame[raw_data] df #Save the dataframe to the current directory df.to_excel[r'Example1.xlsx']
import pandas as pd
# Create a dataframe
raw_data = {'first_name': ['Sam','Ziva','Kia','Robin'],
'degree': ['PhD','MBA','','MS'],
'age': [25, 29, 19, 21]}
df = pd.DataFrame[raw_data]
df
#Save the dataframe to the current directory
df.to_excel[r'Example1.xlsx']
0first_nameage0Sam251Ziva292Kia193Robin214. Các lỗi thường gặp và cách khắc phục sự cố
Liệt kê lỗi phổ biến mà bạn có thể gặp phải khi tải dữ liệu từ tệp CSV vào khung dữ liệu Pandas sẽ là