Server Side Programming Articles

Page 332 of 2109

How to shift a column in a Pandas DataFrame?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 8K+ Views

The shift() method in Pandas allows you to shift the values in a column up or down by a specified number of positions. This is useful for creating lagged variables or aligning time series data. Syntax shift(periods=1, freq=None, axis=0, fill_value=None) Parameters periods − Number of positions to shift. Positive values shift down, negative values shift up. axis − 0 for shifting along rows (default), 1 for shifting along columns. fill_value − Value to use for filling the newly created missing positions. Basic Column Shifting Let's create a DataFrame and demonstrate ...

Read More

How to append two DataFrames in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 88K+ Views

To append the rows of one DataFrame with the rows of another, we can use the Pandas append() function. With the help of append(), we can combine DataFrames vertically. Let's see how to use this method with examples. Note: The append() method is deprecated since Pandas 1.4.0. Use pd.concat() instead for new code. Steps to Append DataFrames Create two DataFrames with data Use append() method or pd.concat() to combine them Set ignore_index=True to reset row indices Handle different column names appropriately Example 1: Appending DataFrames with Same Columns When DataFrames have the same ...

Read More

How to get nth row in a Pandas DataFrame?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 43K+ Views

To get the nth row in a Pandas DataFrame, we can use the iloc[] method. For example, df.iloc[4] will return the 5th row because row numbers start from 0. Syntax df.iloc[n] Where n is the index position (0-based) of the row you want to access. Creating a Sample DataFrame Let's create a DataFrame with student information ? import pandas as pd df = pd.DataFrame({ 'name': ['John', 'Jacob', 'Tom', 'Tim', 'Ally'], 'marks': [89, 23, 100, 56, 90], 'subjects': ["Math", ...

Read More

How to find numeric columns in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 7K+ Views

To find numeric columns in Pandas, we can use the select_dtypes() method to filter columns based on their data types. This method allows us to specify which numeric types to include or exclude from our DataFrame. Basic Example Let's start with a simple example using select_dtypes() ? import pandas as pd # Create a DataFrame with mixed data types df = pd.DataFrame({ 'name': ['John', 'Jacob', 'Tom', 'Tim', 'Ally'], 'marks': [89, 23, 100, 56, 90], 'subjects': ["Math", "Physics", "Chemistry", "Biology", "English"] }) print("Input ...

Read More

Python Pandas – Find the maximum value of a column and return its corresponding row values

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 30K+ Views

To find the maximum value of a column and return its corresponding row values in Pandas, we can use df.loc[df[col].idxmax()]. This method first finds the index of the maximum value using idxmax(), then uses loc[] to retrieve the entire row. Syntax df.loc[df[column_name].idxmax()] Where: df − The DataFrame column_name − The column to find the maximum value in idxmax() − Returns the index of the maximum value loc[] − Selects the row by index Example Let's create a DataFrame and find the maximum values for different columns ? import pandas ...

Read More

How to get the correlation between two columns in Pandas?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 33K+ Views

We can use the .corr() method to get the correlation between two columns in Pandas. The correlation coefficient measures the linear relationship between two variables, ranging from -1 to 1. Basic Syntax # Method 1: Using .corr() on a Series correlation = df['column1'].corr(df['column2']) # Method 2: Using .corr() on DataFrame to get correlation matrix correlation_matrix = df[['column1', 'column2']].corr() Example Let's create a DataFrame and calculate correlations between different columns ? import pandas as pd # Create sample DataFrame df = pd.DataFrame({ "x": [5, 2, 7, 0], ...

Read More

How to filter rows in Pandas by regex?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 18K+ Views

A regular expression (regex) is a sequence of characters that define a search pattern. Pandas provides several methods to filter DataFrame rows using regex patterns, including str.match(), str.contains(), and str.extract(). Using str.match() Method The str.match() method matches regex patterns from the beginning of each string ? import pandas as pd df = pd.DataFrame({ 'name': ['John', 'Jacob', 'Tom', 'Tim', 'Ally'], 'marks': [89, 23, 100, 56, 90], 'subjects': ["Math", "Physics", "Chemistry", "Biology", "English"] }) print("Input DataFrame:") print(df) Input DataFrame: ...

Read More

Python – Pandas Dataframe.rename()

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 4K+ Views

The rename() method in Pandas allows you to change column names in a DataFrame efficiently. You can rename single or multiple columns using dictionary mapping or functions. Basic Syntax The basic syntax for renaming columns is ? DataFrame.rename(columns={old_name: new_name}, inplace=False) Renaming a Single Column Here's how to rename one column using the rename() method ? import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0], "y": [4, ...

Read More

How to access a group of rows in a Pandas DataFrame?

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 3K+ Views

To access a group of rows in a Pandas DataFrame, we can use the loc[] indexer. For example, if we use df.loc[2:5], then it will select all the rows from index 2 to 5 (inclusive). Using loc[] for Row Selection The loc[] method allows label-based indexing and supports slice notation for selecting consecutive rows ? import pandas as pd df = pd.DataFrame( { "x": [5, 2, 7, 0, 7, 0, 5, 2], "y": [4, 7, 5, 1, 5, 1, 4, ...

Read More

Delete the first three rows of a DataFrame in Pandas

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 26-Mar-2026 1K+ Views

To delete the first three rows of a DataFrame in Pandas, we can use the iloc[] indexer to slice the DataFrame starting from the fourth row (index 3). Using iloc[] to Delete First Three Rows The iloc[] method allows positional indexing. By using df.iloc[3:], we select all rows starting from index 3 onwards ? import pandas as pd # Create a DataFrame df = pd.DataFrame( { "x": [5, 2, 7, 0, 7, 0, 5, 2], ...

Read More
Showing 3311–3320 of 21,090 articles
« Prev 1 330 331 332 333 334 2109 Next »
Advertisements