Home > Article > Backend Development > How to read excel data in python
How to read Excel data in Python? You can easily read Excel data by using the read_excel() function in the Pandas library: Import Pandas library: import pandas as pd Use the read_excel() function to read Excel files: data = pd.read_excel('path/to/file .xlsx') to read a specific worksheet: data = pd.read_excel('path/to/file.xlsx', sheet_name='Sheet1') to read a specific
How to read Excel data in Python
How to read Excel data?
To read Excel data, you can use the read_excel()
function in the Pandas library.
Detailed steps:
<code class="python">import pandas as pd</code>
read_excel()
function to read an Excel file: This function needs to provide the path or file object of the Excel file, and can specify other parameters (such as where to read worksheet or column):
<code class="python">data = pd.read_excel('path/to/file.xlsx')</code>
To read a specific worksheet, please enter in sheet_name
Specify the worksheet name or index in the parameter:
<code class="python">data = pd.read_excel('path/to/file.xlsx', sheet_name='Sheet1')</code>
To read specific columns, Please specify the name or index of the column in the usecols
parameter:
<code class="python">data = pd.read_excel('path/to/file.xlsx', usecols=['A', 'C'])</code>
Read data will be stored in a pandas.DataFrame
object, which can be stored in a variable:
<code class="python">df = pd.read_excel('path/to/file.xlsx')</code>
Example:
Read named "example .xlsx" Excel file and store it in the df
variable:
<code class="python">import pandas as pd df = pd.read_excel('example.xlsx', sheet_name='Sheet1')</code>
The above is the detailed content of How to read excel data in python. For more information, please follow other related articles on the PHP Chinese website!