
to_excel (writer, sheet_name = 'Sheet1' ) ExcelWriter ( 'my_file_1.xlsx' ) as writer : read_excel ( 'my_file.xlsx' ) # Creating a copy of workbook If you’d like to save multiple dataframes to the same file, you can use the following syntax: import pandas as pd You can use the ExcelWriter class to get more options while saving to your spreadsheet. to_excel ( 'my_file.xlsx', sheet_name = 'My Sheet' ) More options while saving your spreadsheet The default value of this argument is Sheet1: df. You can save your dataframe as a certain sheet in the workbook using the sheet_name argument. You can also open the same file using the function read_excel(). Then you can create a new spreadsheet file by calling the to_excel() function on the dataframe, specifying the name of the file it should save as: df. Let’s recreate the demo sheet from the top of the article: import pandas as pdĭf = pd. To create a new file, we first need a dataframe.
add styling to the cells in the workbook. The workflow for creating worksheets is similar to that of the previous section. Here’s an article on exploring the values of your Pandas dataframe. You can query your dataset once it’s loaded into a dataframe with the inbuilt functions in Pandas. loc() method, it will search for the label 0 within the index: print (workbook. For instance, if you pass the argument 0 to the. Similarly, you can search for a value using a label through the. iloc() searches for the value at the 0th index location. iloc() method helps you search for a value based on the index location. read_excel ( 'sample-xlsx-file-for-testing.xlsx' ) # Print the 1st value of the Product column print (workbook. Once you’ve selected the worksheet into a dataframe, you can extract the value of a particular data cell by querying into the Pandas dataframe: import pandas as pd read_excel ( '~/Desktop/import-export-data.xlsx', sheet_name = ) Getting data from cells You can also select a number of sheets to be stored as a dict of Pandas dataframes by passing a list the sheet_name argument: # Read the first two sheets and a sheet with the name 'Sheet 3' read_excel ( 'sample-xlsx-file-for-testing.xlsx', sheet_name = 0 ) read_excel ( 'sample-xlsx-file-for-testing.xlsx', sheet_name = 'Sheet1' ) # Read the 1st sheet in the file You can either provide the name of the sheet as a string, or the index of the sheet (starting from 0): # Read the sheet with the name 'Sheet1' By default, the read_excel() function parses the first sheet in the file. You can select a certain sheet from your spreadsheet by using the sheet_name argument. read_excel ( '~/Desktop/import-export-data.xlsx', usecols = 'A:E' )Īdditionally, you can use the nrows and skiprows arguments to read only a certain number of rows, or ignore a certain number of rows at the beginning, respectively. For instance, the following argument would read only the first five columns: workbook = pd. If your spreadsheet is very large, you can add an argument use_cols, which loads only certain columns to the dataframe. read_excel ( 'sample-xlsx-file-for-testing.xlsx', encoding =sys. If your file has non-ASCII characters, you should open it in the unicode format as follows: import sys
Pandas reads the spreadsheet as a table and stores it as a Pandas dataframe.