Articles on Trending Technologies

Technical articles with clear explanations and examples

Merge Python Pandas dataframe with a common column and set NaN for unmatched values

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

To merge two Pandas DataFrames with a common column, use the merge() function and set the on parameter as the column name. To set NaN for unmatched values, use the how parameter and set it to left or right for left or right joins respectively. Understanding Merge Types The how parameter determines which rows to include ? left − Keep all rows from the left DataFrame, set NaN for unmatched right values right − Keep all rows from the right DataFrame, set NaN for unmatched left values ...

Read More

Python – Drop multiple levels from a multi-level column index in Pandas dataframe

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

When working with Pandas DataFrames that have multi-level column indexes, you can drop multiple levels using the columns.droplevel() method repeatedly. This is useful when you need to simplify complex hierarchical column structures. Creating a Multi-Level Column Index First, let's create a DataFrame with a three-level column index using MultiIndex.from_tuples() ? import numpy as np import pandas as pd # Create multi-level column index items = pd.MultiIndex.from_tuples([("Col 1", "Col 1", "Col 1"), ...

Read More

Python – Drop a level from a multi-level column index in Pandas dataframe

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

Multi-level column indexes in Pandas DataFrames provide hierarchical structure for columns. To drop a level from a multi-level column index, use the columns.droplevel() method. This is useful when you want to simplify your DataFrame structure by removing redundant hierarchy levels. Creating a Multi-Level Column Index First, create a multi-level column index using MultiIndex.from_tuples() − import numpy as np import pandas as pd # Create multi-level column index columns = pd.MultiIndex.from_tuples([ ("Sales", "Q1", "Jan"), ("Sales", "Q1", "Feb"), ("Sales", "Q2", "Mar") ]) ...

Read More

Python – Ascending Order Sort grouped Pandas dataframe by group size?

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 574 Views

In Pandas, you can group a DataFrame and sort the groups by their size in ascending order. This is useful for understanding the distribution of data across different categories. Understanding Group Size Sorting To sort grouped DataFrames by group size in ascending order, we combine three methods: groupby() − Groups DataFrame by specified column size() − Returns the count of rows in each group sort_values(ascending=True) − Sorts groups by size in ascending order Basic Example Let's create a DataFrame and sort groups by size in ascending order − import pandas as ...

Read More

Python Pandas - Filtering few rows from a DataFrame on the basis of sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 646 Views

In Pandas, you can filter DataFrame rows based on the sum of values across columns. This is useful when you need to select rows where the total meets specific criteria, such as student marks where the combined score exceeds a threshold. Creating the DataFrame Let's start by creating a DataFrame with student marks across different subjects ? import pandas as pd # Create a DataFrame with 3 students' marks dataFrame = pd.DataFrame({ 'Jacob_Marks': [95, 90, 70, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], ...

Read More

Python Pandas – Fetch the Common rows between two DataFrames with concat()

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 599 Views

To fetch the common rows between two DataFrames, use the concat() function. This method involves concatenating both DataFrames, grouping by all columns, and identifying rows that appear in both DataFrames. Understanding the Approach The process involves several steps ? Concatenate both DataFrames using concat() Reset the index to avoid conflicts Group by all columns to identify duplicates Filter groups with more than one occurrence Step-by-Step Implementation Creating Sample DataFrames Let's create two DataFrames with car data ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ ...

Read More

Python Program – Convert String to matrix having K characters per row

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 812 Views

When it is required to convert a string into a matrix that has K characters per row, we can use string slicing and list comprehension. This technique is useful for formatting text data into fixed-width rows for display or processing. Method 1: Using String Slicing and List Comprehension This approach creates a matrix by slicing the string into chunks of K characters ? def convert_string_to_matrix(text, k): matrix = [] for i in range(0, len(text), k): row = list(text[i:i+k]) ...

Read More

Python – To convert a list of strings with a delimiter to a list of tuple

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 380 Views

Converting a list of strings with delimiters to a list of tuples is a common task in Python. This can be achieved using list comprehension combined with the split() method to break strings at delimiter positions. Basic Example Here's how to convert delimiter-separated strings into tuples of integers ? data_list = ["33-22", "13-44-81-39", "42-10-42", "36-56-90", "34-77-91"] print("Original list:") print(data_list) delimiter = "-" result = [tuple(int(element) for element in item.split(delimiter)) for item in data_list] print("Converted to tuples:") print(result) Original list: ['33-22', '13-44-81-39', '42-10-42', '36-56-90', '34-77-91'] Converted to tuples: [(33, 22), ...

Read More

Python – Descending Order Sort grouped Pandas dataframe by group size?

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

Sorting grouped Pandas DataFrame by group size in descending order is useful for analyzing data distribution. We use groupby() to group data, size() to count group members, and sort_values(ascending=False) to sort in descending order. Creating a Sample DataFrame Let's start by creating a DataFrame with car information ? import pandas as pd # Create dataframe with Car and Registration Price columns dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mercedes', 'Jaguar', 'Bentley'], "Reg_Price": [1000, 1400, 1000, 900, 1700, 900] }) print("DataFrame:") print(dataFrame) DataFrame: ...

Read More

Python – Sort given list of strings by part the numeric part of string

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

When it is required to sort a given list of strings based on the numeric part of the string, we can use regular expressions with the sort() method's key parameter to extract and compare the numeric values. Example Below is a demonstration of sorting strings by their numeric parts ? import re def extract_numeric_part(string): return list(map(int, re.findall(r'\d+', string)))[0] strings = ["pyt23hon", "fu30n", "lea14rn", 'co00l', 'ob8uje3345t'] print("Original list:") print(strings) strings.sort(key=extract_numeric_part) print("Sorted list by numeric part:") print(strings) Original list: ['pyt23hon', 'fu30n', 'lea14rn', 'co00l', 'ob8uje3345t'] Sorted ...

Read More
Showing 3821–3830 of 61,297 articles
« Prev 1 381 382 383 384 385 6130 Next »
Advertisements