Khá nhiều, tôi có mã sau, tạo tệp Excel.
import xlsxwriter
myName = 'hello.xlsx'
wbk = xlsxwriter.Workbook[myName]
Tệp
import os
print [os.getcwd[]+"\\"+myName]
6 được tạo trong cùng một thư mục, trong đó tệp python. Tôi có thể tham khảo đường dẫn tệp dễ dàng như thế này:import os
print [os.getcwd[]+"\\"+myName]
Tuy nhiên, có cách nào để chỉ đường dẫn, chỉ sử dụng
import os
print [os.getcwd[]+"\\"+myName]
7 không? Một cái gì đó như import os
print [os.getcwd[]+"\\"+myName]
8 từ VBA. Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]6, hãy cung cấp danh sách các tên cột được phân tích cú pháp, ví dụ: [Một A, B, D, E Tiết].
Step 1: Take a screenshot of the file path. To begin, you'll need to write down the exact path to the Excel file on your computer....
Step 2: Put the Python code into action. Here's the Python code we used in our example...
Step 3: Import the Excel file using Python code.
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
import os
print [os.getcwd[]+"\\"+myName]
5Nếu bạn muốn nhập một tờ Excel cụ thể, bạn có thể sử dụng công thức sau:
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name'] print [df]
- Làm thế nào kéo dữ liệu từ excel đến python?
- Nếu bạn có một tệp và bạn muốn phân tích dữ liệu trong đó, bạn cần thực hiện những điều sau đây theo thứ tự này:.
- Nhập mô -đun Pandas ..
- Mở tệp bảng tính [hoặc sổ làm việc].
- Chọn một tờ ..
- Trích xuất các giá trị của các ô dữ liệu cụ thể ..
- Python có thể truy cập các tệp excel không?
- Về mặt kỹ thuật, nhiều gói cho phép chúng tôi làm việc với các tệp Excel trong Python. Tuy nhiên, trong hướng dẫn này, chúng tôi sẽ sử dụng các thư viện Pandas và XLRD để tương tác với sổ làm việc Excel. Về cơ bản, bạn có thể nghĩ về một gấu trúc DataFrame như một bảng tính với các hàng và cột được lưu trữ trong các đối tượng loạt.
- Học
- Khóa học
- Theo dõi nghề nghiệp
- Các khóa học sắp tới
- Giấy chứng nhận
- Giấy chứng nhận theo dõi nghề nghiệp
- Giấy chứng nhận khóa học
- Tài nguyên
- Tất cả tài nguyên
- Ghi chú khóa học
- Mẫu
Infographics
Bài kiểm tra thực hành
Blog
Câu chuyện thành công
Giá cả
Việc kinh doanhKế hoạch nhóm
0
Đào tạo trực tiếp
Đăng nhập Đăng ký
Việc kinh doanh
Kế hoạch nhóm
2
Đào tạo trực tiếp
Đăng nhập Đăng ký
Việc kinh doanh
Kế hoạch nhóm
1
Đào tạo trực tiếp
Việc kinh doanh
Giá cả
0
Việc kinh doanh
Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]6, hãy cung cấp danh sách các tên cột được phân tích cú pháp, ví dụ: [Một A, B, D, E Tiết].
5 is the most popular data manipulation package in Python, and DataFrames are the Pandas data type for storing tabular 2D data. Reading data from excel files or CSV files, and writing data to Excel files or
CSV filesusing Python Pandas is a necessary skill for any analyst or data scientist.import os
print [os.getcwd[]+"\\"+myName]
4. Lỗi phổ biến và khắc phục sự cố
- Liệt kê các lỗi phổ biến mà bạn có thể gặp phải trong khi tải dữ liệu từ các tệp CSV vào Pandas DataFrame sẽ là:
- Lý do: Tệp không tìm thấy lỗi thường xảy ra khi có sự cố với đường dẫn tệp [hoặc thư mục] hoặc tên tệp.
- Khắc phục: Kiểm tra đường dẫn tệp, tên tệp và tiện ích mở rộng tệp.
- Lý do: Trong ____ 47 & nbsp; bắt đầu một lối thoát Unicode tám ký tự, chẳng hạn như & nbsp; ________ 48. Trong mã, lối thoát được theo sau bởi nhân vật, không hợp lệ.
- Sử dụng chuỗi tiền tố với & nbsp; ________ 49 & nbsp; [để tạo một chuỗi thô]
0 hoặc,pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
- Sử dụng chuỗi tiền tố với & nbsp; ________ 49 & nbsp; [để tạo một chuỗi thô]
- Bạn cần phải sao chép tất cả các dấu gạch chéo ngược
1pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
- Lý do: & nbsp; XLRD & NBSP; Gói không có sẵn trong môi trường Python
- Lý do: Trong ____ 47 & nbsp; bắt đầu một lối thoát Unicode tám ký tự, chẳng hạn như & nbsp; ________ 48. Trong mã, lối thoát được theo sau bởi nhân vật, không hợp lệ.
- Khắc phục: & nbsp; install & nbsp; xlrd & nbsp; gói nếu bạn gặp lỗi trên
3pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
1. Pandas read_excel [] cú pháp
Chức năng cú pháp của 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]
1 | Đường dẫn tệp IOTHE từ nơi bạn muốn đọc dữ liệu. Đây có thể là một đường dẫn URL hoặc, có thể là một đường dẫn tệp hệ thống cục bộ. Các sơ đồ URL hợp lệ bao gồm HTTP, FTP, S3 và Tệp. the file path from where you want to read the data. This could be a URL path or, could be a local system file path. Valid URL schemes include http, ftp, s3, and file. |
2 | Sheet_name: str, int, list hoặc none, các trường hợp 0available mặc định: ~ mặc định là & nbsp; Tên Tờ Sheet1, ~ Tải nhiều tờ bằng cách sử dụng Dict: ________ 22 sẽ tải đầu tiên, thứ ba và trang tính có tênstr, int, list, or None, default 0 Available cases: ~Default is 9: 1st sheet as a DataFrame~Use import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]0: To read 2nd sheet as a DataFrame ~Use Specific Sheet Name: import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]1 to load sheet with name “Sheet1” ~Load Multiple Sheets using dict: import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]2 will load first, third and sheet named “MySheet” as a dictionary of DataFrame ~None: Load All sheets |
3 | HeaderDefault là 0. Pass Pass tiêu đề = 1 để xem dòng thứ hai của tập dữ liệu là tiêu đề. Không sử dụng nếu không có tiêu đề. default is 0. Pass Header = 1 to consider the second line of the dataset as a header. Use None if there is no header. |
4 | Usecols ~ Mặc định là import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]3, sau đó phân tích tất cả các cột. A, B, E: F Tiết]. Phạm vi bao gồm cả hai bên. ~ Nếu import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]5, chỉ ra danh sách các số cột được phân tích cú pháp, ví dụ: [1,2,5]. ~ Nếu import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]6, cung cấp danh sách các tên cột được phân tích cú pháp, ví dụ: [Một A, B, D, E Tiết]. ~Default is import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]3, then parse all columns. ~If import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]4, then provide a comma-separated list of Excel columns [“A, B, D, E”] or range of Excel columns [e.g. “A:F” or “A, B,E:F”]. Ranges are inclusive of both sides. ~If import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]5, indicates list of column numbers to be parsed e.g. [1,2,5]. ~If import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]6, provide list of column names to be parsed e.g. [“A, B, D, E”]. |
5 | NROWS: Mặc định là ____23Number của các hàng để phân tích cú pháp [cung cấp int].: Default is import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]3 Number of rows to parse [provide int]. |
Để biết danh sách đầy đủ của & nbsp; read_excel & nbsp; các tham số tham khảo & nbsp; tài liệu chính thức.read_excel parameters refer to official documentation.
2. Nhập tệp Excel bằng Python Pandas
Hãy để xem xét một ví dụ đầy đủ:
- Tạo một dataFrame & nbsp; từ đầu và lưu nó 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']
Chúng tôi có dữ liệu sau đây về sinh viên:
Sam | Bằng tiến sĩ | 25 |
Ziva | MBA | 29 |
Kia | 19 | |
Robin | CÔ | 21 |
Đọc tệp Excel vào Pandas DataFrame [giải thích]
Bây giờ, hãy để Lừa xem các bước để nhập tệp Excel vào DataFrame.
Bước 1: & nbsp; Nhập đường dẫn và tên tệp nơi lưu trữ tệp excel. Có thể là một đường dẫn tệp hệ thống cục bộ hoặc đường dẫn URL.Enter the path and filename where the Excel file is stored. The could be a local system file path or URL path.
Ví dụ,
& nbsp;pd.read_excel[r‘D:\Python\Tutorial\Example1.csv‘]
Lưu ý rằng đường dẫn được tô sáng với 3 màu khác nhau:
- & Nbsp; phần màu xanh & nbsp; đại diện cho đường dẫn mà tệp excel được lưu.blue part represents the path where the Excel file is saved.
- Phần màu xanh lá cây tên của tệp bạn muốn nhập.green partis the name of the file you want to import.
- & Nbsp; phần màu tím & nbsp; đại diện cho loại tệp hoặc tiện ích mở rộng tệp excel. Sử dụng ‘.xlsx, trong trường hợp tệp excel & nbsp;.purple part represents the file type or Excel file extension. Use ‘.xlsx’ in case of an Excel file.
Sửa đổi mã python trên để phản ánh đường dẫn nơi lưu trữ tệp excel trên & nbsp; máy tính & nbsp của bạn.
Lưu ý: Bạn có thể lưu hoặc đọc tệp Excel mà không cung cấp rõ ràng đường dẫn tệp [phần màu xanh] bằng cách đặt tệp trong thư mục làm việc hiện tại. Để tìm đường dẫn thư mục hiện tại, sử dụng mã bên dưới: You can save or read an Excel file without explicitly providing a file path [blue part] by placing the file in the current working directory. To find current directory path use below code:
# Current working directory
import os
print[os.getcwd[]]
# Display all files present in the current working directory
print[os.listdir[os.getcwd[]]]
D:\Python\Tutorial\ Example1.xlsx
Tìm hiểu cách đọc nhiều tệp trong một thư mục [thư mục] ở đây.here.
Bước 2: & NBSP; Nhập mã sau và thực hiện các thay đổi cần thiết cho đường dẫn của bạn để đọc tệp Excel. Enter the following code and make the necessary changes to your path to read the Excel file.
import pandas as pd
# Read the excel file
df = pd.read_excel[r'D:\Python\Tutorial\Example1.xlsx']
df
Ảnh chụp nhanh biểu diễn dữ liệu trong các tệp Excel
Ở phía bên trái của tệp excel hình ảnh được mở trong Microsoft Excel. Ở phía bên phải, cùng một tệp Excel được mở trong Juptyter Notebook bằng Pandas Read_Excel.left side of the image Excel file is opened in Microsoft Excel. On the right side same Excel file is opened in Juptyter Notebook using pandas read_excel.
3. Pandasread_excel Các ví dụ thông số quan trọngPandas read_excel Important Parameters Examples
3.1 Nhập bảng Excel cụ thể bằng cách sử dụng Python Pandas
Có thể có nhiều tờ trong một tập tin Excel. Gấu trúc cung cấp các phương pháp khác nhau để nhập một hoặc nhiều bảng excel trong tham số
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]8.
- Mặc định là & nbsp; ________ 19: Đọc tờ 1 trong Excel dưới dạng A & NBSP; DataFrame
- Sử dụng
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
0: Để đọc tờ thứ 2 dưới dạng A & NBSP; DataFrame - Sử dụng tên trang cụ thể:
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
1 để tải bảng có tên - Tải nhiều tờ bằng cách sử dụng Dict: ________ 22 sẽ tải đầu tiên, thứ ba và tờ có tên là MySheet, như một từ điển của & nbsp;
- Không: Tải tất cả các tờ
1. Nhập bảng excel bằng số nguyên
Theo mặc định
3 nhập bảng 1 trong Excel dưới dạng A & NBSP; DataFrame. Để nhập tờ Excel thứ hai, tức là thông tin tiền lương của chúng tôi trong trường hợp của chúng tôi là A & NBSP; Pandas DataFrame sử dụng 4import os
print [os.getcwd[]+"\\"+myName]
0first_name | salarysalary | |
0 | Sam | 120000 |
1 | Ziva | 80000 |
2 | Kia | 110000 |
3 | Robin | 150000 |
CÔ
Đọc tệp Excel vào Pandas DataFrame [giải thích]
Bây giờ, hãy để Lừa xem các bước để nhập tệp Excel vào DataFrame.first_name | bằng cấp | tuổi tác | |
0 | Sam | Bằng tiến sĩ | 25 |
1 | Ziva | MBA | 29 |
2 | Kia | Robin | 19 |
3 | Robin | CÔ | 21 |
Đọc tệp Excel vào Pandas DataFrame [giải thích]
Bây giờ, hãy để Lừa xem các bước để nhập tệp Excel vào DataFrame.
import os
print [os.getcwd[]+"\\"+myName]
2Bước 1: & nbsp; Nhập đường dẫn và tên tệp nơi lưu trữ tệp excel. Có thể là một đường dẫn tệp hệ thống cục bộ hoặc đường dẫn URL.
import os
print [os.getcwd[]+"\\"+myName]
33.2 Chỉ nhập n hàng bảng excel bằng gấu trúc bằng cách sử dụng gấu trúc
Đôi khi tệp Excel khá lớn hoặc hệ thống của chúng tôi có các ràng buộc bộ nhớ. Trong trường hợp này, chúng ta chỉ có thể nhập n hàng trên bảng excel trên cùng bằng cách sử dụng tham số Pandas read_excel
7. Ví dụ: chỉ nhập 2 hàng hàng đầu, sử dụng 8import os
print [os.getcwd[]+"\\"+myName]
4first_name | bằng cấp | tuổi tác | |
0 | Sam | Bằng tiến sĩ | 25 |
1 | Ziva | MBA | 29 |
3.3 Nhập các cột cụ thể của tờ Excel
Có thể có hàng trăm cột trong bảng Excel, nhưng trong khi nhập, chúng tôi chỉ cần vài cột. Trong trường hợp này, chúng ta có thể vượt qua tham số
9. Các cách khác nhau để sử dụng tham số 9 ở bên dưới:- Mặc định là
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
3, phân tích tất cả các cột. - Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
4, thì hãy cung cấp một danh sách các cột Excel được phân tách bằng dấu phẩy [Hồi A, B, D, E Tiết] hoặc phạm vi của các cột Excel [ví dụ: A A: Fiêu hoặc A, B, E: Fiêu]. Phạm vi bao gồm cả hai bên. - Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
5, hãy chỉ ra danh sách các số cột được phân tích cú pháp, ví dụ: [0,2,5]. - Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
6, hãy cung cấp danh sách các tên cột được phân tích cú pháp, ví dụ: [Một A, B, D, E Tiết].
import os
print [os.getcwd[]+"\\"+myName]
5first_name | tuổi tác | |
0 | Sam | 25 |
1 | Ziva | 29 |
2 | MBA | 19 |
3 | 3.3 Nhập các cột cụ thể của tờ Excel | 21 |
Có thể có hàng trăm cột trong bảng Excel, nhưng trong khi nhập, chúng tôi chỉ cần vài cột. Trong trường hợp này, chúng ta có thể vượt qua tham số 9. Các cách khác nhau để sử dụng tham số 9 ở bên dưới:
Mặc định là
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]3, phân tích tất cả các cột.
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name'] print [df]
5- Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
4, thì hãy cung cấp một danh sách các cột Excel được phân tách bằng dấu phẩy [Hồi A, B, D, E Tiết] hoặc phạm vi của các cột Excel [ví dụ: A A: Fiêu hoặc A, B, E: Fiêu]. Phạm vi bao gồm cả hai bên. File Not Found error typically occurs when there is an issue with the file path [or directory] or file name. - Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
5, hãy chỉ ra danh sách các số cột được phân tích cú pháp, ví dụ: [0,2,5]. Check file path, file name, and file extension.
- Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name'] print [df]
6- Nếu
import pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx'] print [df]
6, hãy cung cấp danh sách các tên cột được phân tích cú pháp, ví dụ: [Một A, B, D, E Tiết]. Inimport pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name'] print [df]
7 starts an eight-character Unicode escape, such asimport pandas as pd df = pd.read_excel [r'Path where the Excel file is stored\File name.xlsx', sheet_name='your Excel sheet name'] print [df]
8. In the code, the escape is followed by the character ‘s’, which is invalid. - Fix:
5import os print [os.getcwd[]+"\\"+myName]
- Kia
- Nếu
2pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
- Robin xlrd package is not available in the python environment
- 4. Lỗi phổ biến và khắc phục sự cố Install xlrd package if you get the above error
3pandas.read_excel[io='filepath', sheet_name=0, header=0, usecols=None, nrows=None]
Liệt kê các lỗi phổ biến mà bạn có thể gặp phải trong khi tải dữ liệu từ các tệp CSV vào Pandas DataFrame sẽ là:
Lý do: Tệp không tìm thấy lỗi thường xảy ra khi có sự cố với đường dẫn tệp [hoặc thư mục] hoặc tên tệp.read_excel function.
Khắc phục: Kiểm tra đường dẫn tệp, tên tệp và tiện ích mở rộng tệp.read data from csv files, and write data to CSV filesusing Python.