Hướng dẫn how to add excel file in python - cách thêm file excel vào python

Bạn có thể dễ dàng nhập một tệp excel vào Python bằng gấu trúc. Để thực hiện mục tiêu này, bạn sẽ cần sử dụng read_excel:

import pandas as pd

df = pd.read_excel(r'Path where the Excel file is stored\File name.xlsx')
print(df)

Lưu ý rằng đối với phiên bản Excel trước đó, bạn có thể cần sử dụng phần mở rộng tệp của ‘XLS,

Và nếu bạn có một bảng Excel cụ thể mà bạn muốn nhập, thì bạn có thể áp dụng:Excel sheet that you’d like to import, you may then apply:

import pandas as pd

df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
print(df)

Bây giờ, hãy xem xét một ví dụ bao gồm dữ liệu sẽ được nhập vào Python.

Dữ liệu được nhập vào Python

Giả sử rằng bạn có bảng sau được lưu trữ trong Excel (trong đó tên tệp Excel là ‘Sản phẩm‘):products‘):

product_name giá bán
máy tính700
máy tính bảng250
Máy in120
máy tính xách tay1200
bàn phím100

Sau đó, bạn có thể làm theo các bước dưới đây để nhập tệp Excel vào Python.

Bước 1: Chụp đường dẫn tệp

Đầu tiên, chụp đường dẫn đầy đủ nơi lưu trữ tệp Excel trên máy tính của bạn.

Ví dụ, hãy để giả sử rằng một tệp excel được lưu trữ theo đường dẫn sau:

C: \ Users \ Ron \ Desktop \ Products.xlsxproducts.xlsx

Trong mã Python bên dưới, bạn sẽ cần sửa đổi tên đường dẫn để phản ánh vị trí lưu trữ tệp Excel trên máy tính của bạn.

Don Tiết quên bao gồm tên tệp (trong ví dụ của chúng tôi, nó ‘Sản phẩm‘ như được tô sáng màu xanh). Bạn cũng cần bao gồm phần mở rộng tệp Excel (trong trường hợp của chúng tôi, nó ‘.xlsx‘ như được tô sáng màu xanh lá cây).products‘ as highlighted in blue). You’ll also need to include the Excel file extension (in our case, it’s ‘.xlsx‘ as highlighted in green).

Bước 2: Áp dụng mã Python

Đây là mã Python cho ví dụ của chúng tôi:

import pandas as pd

df = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx')
print(df)

Lưu ý rằng bạn nên đặt ra R R R trước chuỗi đường dẫn để giải quyết các ký tự đặc biệt, chẳng hạn như ‘\. Ngoài ra, don không quên đặt tên tệp ở cuối đường dẫn + .xlsx, that you should place “r” before the path string to address special characters, such as ‘\’. In addition, don’t forget to put the file name at the end of the path + ‘.xlsx’

Bước 3: Chạy mã Python để nhập tệp Excel

Chạy mã Python (được điều chỉnh theo đường dẫn của bạn) và bạn sẽ nhận được bộ dữ liệu sau:

  product_name  price
0     computer    700
1       tablet    250
2      printer    120
3       laptop   1200
4     keyboard    100

Lưu ý rằng bạn có kết quả giống như kết quả được lưu trữ trong tệp Excel.

Lưu ý: Bạn sẽ phải cài đặt một gói bổ sung nếu bạn gặp lỗi sau khi chạy mã:: you’ll have to install an additional package if you get the following error when running the code:

Bước nhập khẩu: Thiếu phụ thuộc tùy chọn ‘XLRD

Sau đó, bạn có thể sử dụng phương pháp cài đặt PIP để cài đặt OpenPyXL cho các tệp .xlsx:

pip install openpyxl

Bước tùy chọn: Chọn tập hợp con của các cột

Bây giờ nếu bạn muốn chọn một cột hoặc cột cụ thể từ tệp Excel?

Ví dụ: nếu bạn muốn chỉ chọn cột sản phẩm_name thì sao? Nếu trường hợp đó, bạn có thể chỉ định tên cột này như đã bị bắt dưới đây:

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name'])
print(df)

Chạy mã (sau khi điều chỉnh đường dẫn tệp) và bạn sẽ chỉ nhận được cột sản phẩm_name:

  product_name
0     computer
1       tablet
2      printer
3       laptop
4     keyboard

Bạn có thể chỉ định các cột bổ sung bằng cách tách tên của họ bằng dấu phẩy, vì vậy nếu bạn muốn bao gồm cả các cột sản phẩm_name và giá, bạn có thể sử dụng cú pháp này:

import pandas as pd

data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
df = pd.DataFrame(data, columns=['product_name', 'price'])
print(df)

Bạn cần phải đảm bảo rằng các tên cột được chỉ định trong mã khớp chính xác với các tên cột trong tệp Excel. Nếu không, bạn sẽ nhận được các giá trị NAN.

Sự kết luận

Bạn chỉ thấy cách nhập một tệp excel vào Python bằng gấu trúc.

Đôi khi, bạn có thể cần nhập tệp CSV vào Python. Nếu trường hợp đó, bạn có thể muốn kiểm tra hướng dẫn sau đây rằng & nbsp; giải thích cách nhập tệp CSV vào Python bằng Pandas.

Bạn cũng có thể kiểm tra tài liệu Pandas & nbsp; để tìm ra & nbsp; thêm về các tùy chọn khác nhau mà bạn có thể áp dụng liên quan đến read_excel.

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    ĐọcPandas provides us the functions to convert datasets in other formats to the Data frame. An excel file has a ‘.xlsx’ format. 

    Bàn luận

    pip install pandas
    pip install xlrd
    

    Không phải lúc nào cũng có thể có được bộ dữ liệu ở định dạng CSV. Vì vậy, Pandas cung cấp cho chúng tôi các chức năng để chuyển đổi bộ dữ liệu ở các định dạng khác thành khung dữ liệu. Một tệp Excel có định dạng ‘.xlsx. & NBSP;pandas.read_excel() function.

    Trước khi chúng tôi bắt đầu, & nbsp; chúng tôi cần cài đặt một vài thư viện. & Nbsp; pandas.read_excel(io, sheet_name=0, header=0, names=None,….)

    & nbsp; Để nhập một tệp excel vào python bằng pandas, chúng ta phải sử dụng chức năng pandas.read_excel (). DataFrame or dict of DataFrames.

    Cú pháp: pandas.read_excel (IO, sheet_name = 0, header = 0, name = none,

    Trả lại: DataFrame hoặc Dict of DataFrames.

    Hãy giả sử tệp Excel trông như thế này:Read an Excel file.

    Python3

    Bây giờ, chúng ta có thể đi sâu vào mã. & NBSP;

    Ví dụ 1: Đọc một tệp excel.

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    6
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    7

    Output:

    Hướng dẫn how to add excel file in python - cách thêm file excel vào python

    import

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    0To select a particular column, we can pass a parameter “index_col“. 

    Python3

    Bây giờ, chúng ta có thể đi sâu vào mã. & NBSP;

    Ví dụ 1: Đọc một tệp excel.

    import

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    0

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    1
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    2
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    3
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    4
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    5

    Output:

    Hướng dẫn how to add excel file in python - cách thêm file excel vào python

    Ví dụ 2: Để chọn một cột cụ thể, chúng ta có thể vượt qua một tham số chỉ số index_col. & Nbsp;In case you don’t prefer the initial heading of the columns, you can change it to indexes using the parameter “header”.

    Python3

    Bây giờ, chúng ta có thể đi sâu vào mã. & NBSP;

    Ví dụ 1: Đọc một tệp excel.

    import

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    0

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    6
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    7

    Output:

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    1
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    2
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    3
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    4
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    5
    If you want to change the data type of a particular column you can do it using the parameter “dtype“.

    Python3

    Bây giờ, chúng ta có thể đi sâu vào mã. & NBSP;

    Ví dụ 1: Đọc một tệp excel.

    import

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    0

      product_name
    0     computer
    1       tablet
    2      printer
    3       laptop
    4     keyboard
    
    1
      product_name
    0     computer
    1       tablet
    2      printer
    3       laptop
    4     keyboard
    
    2
      product_name
    0     computer
    1       tablet
    2      printer
    3       laptop
    4     keyboard
    
    3
      product_name
    0     computer
    1       tablet
    2      printer
    3       laptop
    4     keyboard
    
    4
      product_name
    0     computer
    1       tablet
    2      printer
    3       laptop
    4     keyboard
    
    5

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    6
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    7

    Output:

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    1
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    2
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    3
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    4
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    5
    In case you have unknown values, then you can handle it using the parameter “na_values“. It will convert the mentioned unknown values into “NaN” 

    Python3

    Bây giờ, chúng ta có thể đi sâu vào mã. & NBSP;

    Ví dụ 1: Đọc một tệp excel.

    import pandas as pd
    
    df = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx')
    print(df)
    
    5
    import pandas as pd
    
    data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
    df = pd.DataFrame(data, columns=['product_name', 'price'])
    print(df)
    
    6
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    2
    import pandas as pd
    
    data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
    df = pd.DataFrame(data, columns=['product_name', 'price'])
    print(df)
    
    8
    import pandas as pd
    
    data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
    df = pd.DataFrame(data, columns=['product_name', 'price'])
    print(df)
    
    9
    import pandas as pd
    
    data = pd.read_excel(r'C:\Users\Ron\Desktop\products.xlsx') 
    df = pd.DataFrame(data, columns=['product_name'])
    print(df)
    
    2

    pip install pandas
    pip install xlrd
    
    1
    pip install pandas
    pip install xlrd
    
    2
    pip install pandas
    pip install xlrd
    
    3

    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    6
    import pandas as pd
    
    df = pd.read_excel(r'Path of Excel file\File name.xlsx', sheet_name='your Excel sheet name')
    print(df)
    
    7

    Output: