To find the uncommon rows between two DataFrames, you can use concat() combined with drop_duplicates(). This approach concatenates both DataFrames and removes duplicate rows, leaving only the uncommon ones. Syntax pd.concat([df1, df2]).drop_duplicates(keep=False) Where keep=False removes all occurrences of duplicated rows, leaving only the unique rows from each DataFrame. Example Let's create two DataFrames with car data and find the uncommon rows ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Reg_Price": [1000, 1500, ... Read More
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
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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance