Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Compare specific Timestamps for a Pandas DataFrame – Python
To compare specific timestamps in a Pandas DataFrame, you can access individual rows using index numbers and calculate the difference between timestamp columns. This is useful for analyzing time intervals between related events. Creating a DataFrame with Timestamps First, let's create a DataFrame containing timestamp data ? import pandas as pd # Create a DataFrame with timestamp columns dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), ...
Read MorePython Program to replace list elements within a range with a given number
When working with Python lists, you may need to replace multiple elements within a specific range with the same value. This can be efficiently accomplished using list slicing combined with list multiplication. Syntax The basic syntax for replacing elements within a range is ? list[start:end] = [new_value] * (end - start) Where: start − starting index (inclusive) end − ending index (exclusive) new_value − the value to replace with Basic Example Here's how to replace elements at indices 4 to 7 with the number 9 ? numbers = ...
Read MorePython Program to assign each list element value equal to its magnitude order
When it is required to assign each list element value equal to its magnitude order, we can use the set operation, zip method and list comprehension. The magnitude order represents the rank of each element when sorted in ascending order. Understanding Magnitude Order Magnitude order assigns ranks based on sorted values. The smallest element gets rank 0, the next smallest gets rank 1, and so on. Example Below is a demonstration of assigning magnitude order to list elements ? my_list = [91, 42, 27, 39, 24, 45, 53] print("The list is :") print(my_list) ...
Read MorePython - Filter Supersequence Strings
When we need to filter strings that contain all characters from a given subsequence, we use a list comprehension with the all() function. This technique finds supersequence strings − strings that contain every character from a target substring. What is a Supersequence? A supersequence is a string that contains all characters of another string (subsequence), though not necessarily in consecutive order. For example, "alwaysgreat" is a supersequence of "ys" because it contains both 'y' and 's'. Example Here's how to filter strings containing all characters from a given substring − my_list = ["Python", "/", ...
Read MorePython - Maximum difference across lists
When it is required to find the maximum difference across the lists, the abs() and the max() methods are used to calculate the absolute difference between corresponding elements and find the largest one. Example Below is a demonstration of the same − my_list_1 = [7, 9, 1, 2, 7] my_list_2 = [6, 3, 1, 2, 1] print("The first list is :") print(my_list_1) print("The second list is :") print(my_list_2) my_result = max(abs(my_list_2[index] - my_list_1[index]) for index in range(len(my_list_1))) print("The maximum difference among the lists is :") print(my_result) Output ...
Read MorePython - Remove positional rows
When you need to remove rows from a list by their positions, you can use the pop() method with iteration. The key is to iterate in reverse order to avoid index shifting issues when removing multiple elements. Example Below is a demonstration of removing rows at specific positions − my_list = [[31, 42, 2], [1, 73, 29], [51, 3, 11], [0, 3, 51], [17, 3, 21], [1, 71, 10], [0, 81, 92]] print("The list is:") print(my_list) my_index_list = [1, 2, 5] for index in my_index_list[::-1]: my_list.pop(index) print("The output ...
Read MorePython – Strip whitespace from a Pandas DataFrame
To strip whitespace from a Pandas DataFrame, use the str.strip() method on string columns. This removes both leading and trailing whitespace characters from text data. Importing Pandas First, let us import the required Pandas library with an alias − import pandas as pd Creating a DataFrame with Whitespace Let's create a DataFrame with 3 columns where the first column has leading and trailing whitespaces − import pandas as pd dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], ...
Read MorePython program to compute the power by Index element in List
When it is required to compute the power by index element in a list, we can use iteration along with the ** operator to raise each element to the power of its index position. Understanding the Concept In this operation, each element at index i is raised to the power i. For example, element at index 0 becomes element ** 0 = 1, element at index 1 becomes element ** 1 = element, and so on. Using enumerate() with List Iteration The most straightforward approach uses enumerate() to get both index and element ? ...
Read MorePython - Filter dictionaries by values in Kth Key in list
When working with a list of dictionaries, you might need to filter them based on whether a specific key's value appears in a given list. This can be accomplished using simple iteration or more Pythonic approaches like list comprehension. Using For Loop Method The basic approach iterates through each dictionary and checks if the target key's value exists in the search list ? data_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, ...
Read MorePython - Most common Combination in Matrix
When it is required to find the most common combination in a matrix, a simple iteration, along with the sort() method and Counter method is used. Example Below is a demonstration of the same − from collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print("The list is :") print(my_list) my_result = Counter() for elem in my_list: if len(elem) < 2: continue elem.sort() ...
Read More