Share đọc file excel trong python

xuanlac131

New member
#Python #Excel #Read #Pandas #data

** Cách đọc tệp Excel trong Python **

Excel là một chương trình bảng tính phổ biến được sử dụng để lưu trữ và sắp xếp dữ liệu.Python là một ngôn ngữ lập trình mạnh mẽ có thể được sử dụng để thực hiện nhiều tác vụ khác nhau, bao gồm đọc và viết các tệp Excel.Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách đọc một tệp Excel trong Python bằng thư viện Pandas.

## Điều kiện tiên quyết

Để làm theo với hướng dẫn này, bạn sẽ cần những điều sau đây:

* Python 3 được cài đặt trên máy tính của bạn
* Thư viện Pandas đã cài đặt
* Một tệp Excel mà bạn muốn đọc

## Đọc một tệp excel với gấu trúc

Thư viện Pandas cung cấp một số chức năng để đọc và viết các tệp Excel.Hàm được sử dụng phổ biến nhất để đọc các tệp excel là hàm `read_excel ()`.Hàm này lấy một đường dẫn tệp làm đối số đầu tiên và một số đối số tùy chọn.

Mã sau đây cho thấy cách đọc tệp excel bằng hàm `read_excel ()`:

`` `Python
nhập khẩu gấu trúc dưới dạng PD

# Đọc tệp Excel
df = pd.read_excel ("data.xlsx")

# In năm hàng đầu tiên của DataFrame
in (df.head ())
`` `

Mã này sẽ đọc tệp excel `data.xlsx` và tạo một đối tượng pandas dataFrame.Hàm `head ()` in năm hàng đầu tiên của dataFrame.

## Đọc các tờ cụ thể từ tệp Excel

Theo mặc định, hàm `read_excel ()` sẽ đọc tờ đầu tiên trong một tệp excel.Tuy nhiên, bạn cũng có thể sử dụng đối số `sheet_name` để chỉ định tên của trang tính mà bạn muốn đọc.

Mã sau đây cho thấy cách đọc trang tính có tên "Sheet2" từ tệp Excel:

`` `Python
df = pd.read_excel ("data.xlsx", sheet_name = "sheet2")

# In năm hàng đầu tiên của DataFrame
in (df.head ())
`` `

## Đọc nhiều tờ từ tệp Excel

Bạn cũng có thể sử dụng hàm `read_excel ()` để đọc nhiều bảng từ tệp excel.Để làm điều này, bạn chỉ cần chuyển một danh sách các tên bảng cho đối số `sheet_name`.

Mã sau đây cho thấy cách đọc các tờ có tên "Sheet1" và "Sheet2" từ tệp Excel:

`` `Python
df1 = pd.read_excel ("data.xlsx", sheet_name = "sheet1")
df2 = pd.read_excel ("data.xlsx", sheet_name = "sheet2")

# In năm hàng đầu tiên của DataFrames
in (df1.head ())
in (df2.head ())
`` `

## Phần kết luận

Trong hướng dẫn này, chúng tôi đã chỉ cho bạn cách đọc một tệp Excel trong Python bằng thư viện Pandas.Chúng tôi đã đề cập đến cách đọc một tờ duy nhất từ tệp Excel, cách đọc nhiều bảng từ tệp Excel và cách đọc các tờ cụ thể từ tệp Excel.

## hashtags

* #Python
* #Excel
* #Đọc
* #Pandas
* #dữ liệu
=======================================
#Python #Excel #Read #Pandas #data

**How to Read an Excel File in Python**

Excel is a popular spreadsheet program that is used to store and organize data. Python is a powerful programming language that can be used to perform a variety of tasks, including reading and writing Excel files. In this tutorial, we will show you how to read an Excel file in Python using the pandas library.

## Prerequisites

To follow along with this tutorial, you will need the following:

* Python 3 installed on your computer
* The pandas library installed
* An Excel file that you want to read

## Reading an Excel File with pandas

The pandas library provides a number of functions for reading and writing Excel files. The most commonly used function for reading Excel files is the `read_excel()` function. This function takes a file path as its first argument and a number of optional arguments.

The following code shows how to read an Excel file using the `read_excel()` function:

```python
import pandas as pd

# Read the Excel file
df = pd.read_excel("data.xlsx")

# Print the first five rows of the DataFrame
print(df.head())
```

This code will read the Excel file `data.xlsx` and create a pandas DataFrame object. The `head()` function prints the first five rows of the DataFrame.

## Reading Specific Sheets from an Excel File

By default, the `read_excel()` function will read the first sheet in an Excel file. However, you can also use the `sheet_name` argument to specify the name of the sheet that you want to read.

The following code shows how to read the sheet named "Sheet2" from an Excel file:

```python
df = pd.read_excel("data.xlsx", sheet_name="Sheet2")

# Print the first five rows of the DataFrame
print(df.head())
```

## Reading Multiple Sheets from an Excel File

You can also use the `read_excel()` function to read multiple sheets from an Excel file. To do this, you simply pass a list of sheet names to the `sheet_name` argument.

The following code shows how to read the sheets named "Sheet1" and "Sheet2" from an Excel file:

```python
df1 = pd.read_excel("data.xlsx", sheet_name="Sheet1")
df2 = pd.read_excel("data.xlsx", sheet_name="Sheet2")

# Print the first five rows of the DataFrames
print(df1.head())
print(df2.head())
```

## Conclusion

In this tutorial, we showed you how to read an Excel file in Python using the pandas library. We covered how to read a single sheet from an Excel file, how to read multiple sheets from an Excel file, and how to read specific sheets from an Excel file.

## Hashtags

* #Python
* #Excel
* #Read
* #Pandas
* #data
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top