How to Reverse Row Order in Pandas Dataframes

This tutorial shows two methods to reverse row order based on the dataframe index. The following shows the key syntax code. Method 1: df_name[::-1.] Method 2: df_name.loc[::-1.] Example 1 for Method 1 We can reverse row order in Python Pandas dataframes using car_data[::-1]. Below shows the original dataframe and the reversed dadaframe based on the … Read more

Select Rows based on values in multiple columns in Pandas Dataframes

To select rows based on values in multiple columns in Pandas dataframes, you can use loc() and query(). The following shows the basic syntax of two methods to do so. Method 1: df.loc[(df[‘column1’]==value1) & (df[‘column2’]==value2) ] Method 2: df.query(‘column1==”value1″ & column2==”value2″‘) Example for Method 1 The following is an example showing how to select rows … Read more

Pandas: Concat Two Dataframes

This tutorial shows how to use function of concat() in Python with examples. pd.concat() can be used to concat two dataframes in Pandas. The syntax of combining two dataframes (df_1, and df_2) by adding columns: pd.concat ( [df_1, df_2], axis=1) The syntax of combining two dataframes by adding rows: pd.concat ( [df_1, df_2], axis=0) Example … Read more

How to Sum Rows in Dataframe in Python

This tutorial shows how to sum rows in a dataframe using Pandas in Python. Example 1: Use sum() for all rows Brand Location Number 0 BrandA CA 20 1 BrandB CA 30 2 BrandA NY 25 3 BrandC MA 20 4 BrandA CA 20 115 115 Example 2: Use sum() for selected rows The following … Read more

nltk: How to Remove Stop words in Python

This tutorial shows how you can remove stop words using nltk in Python. Stop words are words not carrying important information, such as propositions (“to”, “with”), articles (“an”, “a”, “the”), or conjunctions (“and”, “or”, “but”). We first need to import the needed packages. We can then set the language to be English. Before removing stop … Read more

How to Reorder Columns in DataFrame in Pandas Python

This tutorial shows how to reorder columns in dataframe using Pandas in Python. This tutorial includes 3 examples using the methods of reindex(), double brackets [[]], and pop(). Sample Dataframe The following is the sample dataframe in Python, which will be later in all examples in this tutorial. The following is the final sample dataframe. … Read more

How to Use Pandas Melt() Function

This short tutorial shows you how you can use melt() funtion in Pandas. It is often used when we need to change the format of dataframe to fit into a certain statistical functions. Example 1 of Using melt() City1 City2 City3 0 6 2 4 1 2 1 1 2 3 3 2 3 4 … Read more