Pandas: Select Two Columns from a Dataframe (Examples)

There are at least 3 methods to select 2 or more than 2 columns from a dataframe in Python. Method 1: Use a list of column names df[ [ “col1”, “col2” ] ] Method 2: Use a range of column index df.iloc [ : , 0 : 2 ] Method 3: Use specific column indexes … Read more

Use sklearn for Linear Regression in Python

Introduction We can use sklearn.linear_model.LinearRegression to do linear regression in Python. The following is the core syntax of using sklearn. lm.fit(IVs, DV) Where, IVs: the independent variables DV: the dependent variable Example for Linear Regression Model The following is the linear regression model, including household income as IVs and purchase intention as DV. The following … Read more

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