Articles on Trending Technologies

Technical articles with clear explanations and examples

Python Program to return the Length of the Longest Word from the List of Words

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

When it is required to return the length of the longest word from a list of words, a method is defined that takes a list as parameter. It iterates through each word to find the maximum length and returns it. Example Below is a demonstration of the same ? def find_longest_length(word_list): max_length = len(word_list[0]) temp = word_list[0] for element in word_list: if len(element) > max_length: ...

Read More

Python Program to Sort Matrix Rows by summation of consecutive difference of elements

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 288 Views

This article demonstrates how to sort matrix rows by the summation of consecutive differences between adjacent elements. We calculate the absolute difference between consecutive elements in each row, sum them up, and use this sum as the sorting key. Understanding Consecutive Differences For a row like [97, 6, 47, 3], the consecutive differences are − |6 - 97| = 91 |47 - 6| = 41 |3 - 47| = 44 Sum = 91 + 41 + 44 = 176 Example Here's a complete program that sorts matrix rows by their consecutive difference summation ...

Read More

Python – Append given number with every element of the list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 471 Views

When you need to add a specific number to every element in a list, Python offers several approaches. The most common method uses list comprehension, which provides a concise and readable solution. Using List Comprehension List comprehension allows you to create a new list by applying an operation to each element ? numbers = [25, 36, 75, 36, 17, 7, 8, 0] print("Original list:") print(numbers) append_value = 6 result = [x + append_value for x in numbers] print("List after appending", append_value, "to each element:") print(result) Original list: [25, 36, 75, ...

Read More

Python – Cumulative Row Frequencies in a List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 431 Views

When you need to count how many times specific elements appear in each row of a nested list, you can use Python's Counter class from the collections module combined with list comprehension to calculate cumulative row frequencies. What are Cumulative Row Frequencies? Cumulative row frequencies refer to counting the total occurrences of specified elements within each row of a nested list. For example, if you want to find how many times elements [19, 2, 71] appear in each row, you sum their individual frequencies per row. Example Here's how to calculate cumulative row frequencies using Counter ...

Read More

Python Program To Get Minimum Element For String Construction

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 243 Views

When you need to find the minimum number of strings required to construct a target string, you can use Python's combinations from itertools along with set operations. This approach finds the smallest subset of strings that contains all characters needed for the target string. Example Below is a demonstration of finding the minimum elements needed ? from itertools import combinations my_list = ["python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_target_str = "onis" my_result = -1 my_set_string = set(my_target_str) complete_val = False for value in range(0, len(my_list) + 1): ...

Read More

Python - Filter Pandas DataFrame with numpy

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

The numpy.where() method can be used to filter Pandas DataFrame by returning indices that meet specified conditions. This approach is particularly useful when you need to apply multiple filtering conditions simultaneously. Setup First, let's import the required libraries and create a sample DataFrame ? import pandas as pd import numpy as np # Create a DataFrame with product records 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 - Summing all the rows of a Pandas Dataframe

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

To sum all the rows of a DataFrame, use the sum() function with axis=1. Setting the axis parameter to 1 adds values across columns for each row, while axis=0 (default) would sum down columns. Creating a Sample DataFrame Let's start by creating a DataFrame with stock data ? import pandas as pd dataFrame = pd.DataFrame({ "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("DataFrame...") print(dataFrame) DataFrame... Opening_Stock Closing_Stock 0 ...

Read More

Python Pandas - How to append rows to a DataFrame

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

To append rows to a DataFrame, use the concat() method (recommended) or the deprecated append() method. Here, we will create two DataFrames and append one after the other. Using concat() Method (Recommended) The concat() method is the modern approach for appending DataFrames ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Jaguar'] }) print("DataFrame1 ...", dataFrame1) print("DataFrame1 length =", len(dataFrame1)) # Create DataFrame2 dataFrame2 = pd.DataFrame({ "Car": ['Mercedes', 'Tesla', 'Bentley', 'Mustang'] }) print("DataFrame2 ...", dataFrame2) print("DataFrame2 length =", ...

Read More

Python Pandas - Create a subset by choosing specific values from columns based on indexes

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 552 Views

To create a subset by choosing specific values from columns based on indexes, use the iloc() method. The iloc() method allows you to select data by integer position, making it perfect for choosing specific rows and columns from a DataFrame. Creating a DataFrame Let us first create a sample DataFrame with product records ? import pandas as pd dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("DataFrame...", dataFrame) ...

Read More

Python Pandas - Subset DataFrame by Column Name

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 672 Views

To create a subset of DataFrame by column name, use the square brackets. Use the DataFrame with square brackets (indexing operator) and the specific column name like this ? dataFrame['column_name'] Syntax # Single column - returns a Series dataFrame['column_name'] # Multiple columns - returns a DataFrame dataFrame[['col1', 'col2']] Example - Single Column Subset Let us create a DataFrame and extract a single column ? import pandas as pd # Create a Pandas DataFrame with Product records dataFrame = pd.DataFrame({ "Product": ["SmartTV", "ChromeCast", "Speaker", ...

Read More
Showing 3791–3800 of 61,299 articles
« Prev 1 378 379 380 381 382 6130 Next »
Advertisements