Pandas: Read All Sheets in Excel

This tutorial shows how you can read multiple sheets in Excel in Python. The following shows the two major steps. Step 1: Read all sheets as a dictionary You can read all sheets in Excel altogether using the parameter of sheet_name=None in pd.read_excel(). Without this, it will only read the first sheet, by default. The … Read more

How to Convert Index as Column in Pandas

This tutorial shows how you can convert dataframe index as a column in Python. The basic syntax is to use reset_index() (see below). df_name.reset_index() Starting Dataframe for Examples 1 and 2 The following is the sample dataframe that will be used in Examples 1 and 2. (Note that, Example 3 will use a different dataframe.) … Read more

How to Clear or Empty a List in Python (Examples)

This tutorial shows 2 methods to clear a list in Python using examples. Method 1: Use clear() to empty a list You can use the method of clear() to empty a list in Python. The following is an example. [‘Tesla’, ‘Ford’, ‘Toyota’] [] Method 2: Use Del to clear or empty a list You can … Read more

How to Read Excel with Multiple Sheets with Pandas (Python)

This tutorial shows how you can use read_excel() read Excel files with multiple sheets. The following is the illustration for sheet1 and sheet2. They have the same data structure, but in 2 different sheets. You can download the Excel file from Github by clicking this link. Method 1: Just read the first sheet Use read_excel() … Read more

What is the difference between `sep` and `delimiter` attributes in read_csv() and read_table() in Pandas

This tutorial explains the difference between sep and delimiter in read_csv() and read_table() in Pandas. In short, `sep` and `delimiter` are the same in both read_csv() and read_table() functions in Pandas. You can use either one of them. In both function description, you can see the following statement. delimiterstr, default None Alias for sep. Brand … Read more

How to Read Text (txt) Files in Pandas

This tutorial uses example Python codes to show 2 methods to read a text (txt) file into the Python programming environment. The following is the screenshot of the txt file. Method 1: Use read_csv() function to read txt You can use read_csv() function to read txt files as well. The basic syntax structure is as … Read more

How to Create a Contingency Table in Pandas

Introduction of crosstab() function You can use the pandas.crosstab() function to create a contingency table. It computes a simple cross tabulation of two (or more) factors. The following is the sample data Brand Location Number 0 Brand 1 CA 200 1 Brand 1 CA 20 2 Brand 2 CA 300 3 Brand 1 NY 400 4 Brand … Read more

How to Combine Pandas Dataframe and Numpy Matrix

You can combine Pandas dataframes and Numpy Matrices by using the pd.concat() function in Pandas. pd.concat([df,pd.DataFrame(Matrix)],axis=1) The following are the steps to combine Pandas dataframe and Numpy matrix. Step 1: Generate a dataframe The following is to generate a dataframe and a matrix first. The following is the print out of the dataframe. Dataframe: Brand … Read more

How to Add Numpy Arrays to a Pandas DataFrame

You can add a NumPy array as a new column to Pandas dataframes by using the tolist() function. The following are the syntax statement as well as examples showing how to actually do it. df[‘new_column_name’] = array_name.tolist() Step 1: Generate a sample dataframe The following is to generate a dataframe and then print it out. … Read more

How to Use Lambda Functions in Python (Pandas)

This short tutorial aims to show how you can use Lambda functions in Python, and especially in Pandas. Introduction The following is the basic structure of Lambda functions: lambda bound_variable: function_or_expression Lambda functions can have any number of arguments but only one expression, typically in one-line expression. The following is an example of adding the number … Read more