Python Articles

Page 147 of 855

Python - First occurrence of one list in another

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 420 Views

When it is required to find the first occurrence of one list in another list, Python provides several approaches. The most efficient method uses the set data structure for fast lookups combined with the next() function for early termination. Using next() with Set Conversion This approach converts the second list to a set for O(1) lookup time and uses next() to return the first match ? my_list_1 = [23, 64, 34, 77, 89, 9, 21] my_list_2 = [64, 10, 18, 11, 0, 21] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) ...

Read More

Python - Merge Pandas DataFrame with Inner Join

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To merge Pandas DataFrame, use the merge() function. An inner join returns only the rows that have matching values in both DataFrames. It's implemented by setting the how parameter to "inner". Syntax pd.merge(left, right, on='column_name', how='inner') How Inner Join Works An inner join combines rows from two DataFrames where the join condition is met. Only matching records from both DataFrames are included in the result. Creating Sample DataFrames Let's create two DataFrames with some common and different car models ? import pandas as pd # Create DataFrame1 dataFrame1 = ...

Read More

Python - Calculate the variance of a column in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To calculate the variance of column values in a Pandas DataFrame, use the var() method. Variance measures how spread out the data points are from the mean value. Syntax The basic syntax for calculating variance is ? DataFrame['column_name'].var() Creating a DataFrame First, import the required Pandas library and create a DataFrame ? import pandas as pd # Create DataFrame with car data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") ...

Read More

Python - How to reset index after Groupby pandas?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 9K+ Views

When you perform a groupby operation in pandas, the grouped column becomes the index. To convert this index back to a regular column, use reset_index(). Import Required Library First, import pandas ? import pandas as pd Creating Sample DataFrame Let's create a DataFrame with car names and prices ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Audi", "Mercedes", "Audi", "Lexus", "Mercedes", "Lexus", "Mercedes"], "Reg_Price": [1000, 1400, 1100, 900, 1700, 1800, 1300, 1150, 1350] }) print("Original DataFrame:") print(dataFrame) ...

Read More

Python - Sum only specific rows of a Pandas Dataframe

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

To sum only specific rows in a Pandas DataFrame, use the loc[] method to select specific row ranges and columns. The loc[] method allows you to specify both row indices and column names for precise data selection. Let's start by creating a DataFrame with product inventory data ? import pandas as pd # Create DataFrame with product inventory data dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("Original DataFrame:") print(dataFrame) ...

Read More

Python Pandas – Find the common rows between two Data Frames

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 5K+ Views

To find the common rows between two DataFrames in Pandas, use the merge() method with how='inner'. This returns only the rows that have identical values across all columns in both DataFrames. Creating Sample DataFrames Let us first create two DataFrames with car data ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) DataFrame1: Car Units 0 ...

Read More

Python Pandas – Check if any specific column of two DataFrames are equal or not

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 434 Views

To check if any specific column of two DataFrames are equal or not, use the equals() method. This method compares both the values and the structure of the columns, returning True if they are identical. Creating Sample DataFrames Let us first create DataFrame1 with two columns − import pandas as pd dataFrame1 = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) ...

Read More

Python - Calculate the mean of column values of a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 822 Views

To calculate the mean of column values in a Pandas DataFrame, use the mean() method. This method computes the arithmetic average of numeric columns, making it essential for data analysis tasks. Basic Syntax The basic syntax for calculating column mean is ? # For a single column dataframe['column_name'].mean() # For all numeric columns dataframe.mean() Creating Sample DataFrames Let's create two DataFrames to demonstrate mean calculations ? import pandas as pd # Create DataFrame1 with car data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', ...

Read More

Python - Create a Pipeline in Pandas

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 350 Views

To create a pipeline in Pandas, we use the pipe() 100] def add_category(df): df['CATEGORY'] = 'Premium' return df # Create DataFrame df = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) # Chain multiple operations result = (df.pipe(uppercase_columns) .pipe(filter_high_units) .pipe(add_category)) print("Final result after pipeline:") print(result) ...

Read More

Python Pandas and Numpy - Concatenate multiindex into single index

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 591 Views

To concatenate a multiindex into a single index in Pandas, we can use the map() method with join() to combine tuple elements with a separator. Let us start by importing the required libraries ? Import Libraries import pandas as pd import numpy as np Creating a Series with Tuple Index First, we create a Pandas Series with tuples as index values ? # Create tuples for multiindex index_tuples = [('Jacob', 'North'), ('Ami', 'East'), ('Ami', 'West'), ...

Read More
Showing 1461–1470 of 8,549 articles
« Prev 1 145 146 147 148 149 855 Next »
Advertisements