read_xlsxでシート指定
When using the read_excel function in pandas to read an Excel file, you can specify the sheet to read by using the sheet_name parameter. The sheet_name parameter can take various values:
-
Sheet Name: If your sheet is named, you can provide the sheet name as a string.
python
import pandas as pd # Specify the sheet name sheet_name = 'Sheet1' # Read the Excel file with specified sheet name df = pd.read_excel('your_file.xlsx', sheet_name=sheet_name) -
Sheet Index: You can also use the sheet index, where 0 represents the first sheet.
python
import pandas as pd # Specify the sheet index sheet_index = 0 # Read the Excel file with specified sheet index df = pd.read_excel('your_file.xlsx', sheet_name=sheet_index) -
Multiple Sheets: If you want to read multiple sheets into a dictionary of DataFrames, you can provide a list of sheet names or indices.
python
import pandas as pd # Specify multiple sheet names or indices sheet_names = ['Sheet1', 'Sheet2'] # Read the Excel file with specified sheet names dfs = pd.read_excel('your_file.xlsx', sheet_name=sheet_names) # Access DataFrames using sheet names as keys df_sheet1 = dfs['Sheet1'] df_sheet2 = dfs['Sheet2']
Choose the appropriate method based on your Excel file’s structure and your specific needs. If you are working with an Excel file with only one sheet, you can omit the sheet_name parameter, and pandas will read the first sheet by default.

ディスカッション
コメント一覧
まだ、コメントがありません