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
Python – Remove Rows for similar Kth column element
When working with lists of lists in Python, you may need to remove rows that have duplicate values in a specific column (Kth position). This can be achieved using simple iteration and the append() method to build a new list with unique values. Example Below is a demonstration of removing rows with duplicate Kth column elements − data = [[45, 95, 26], [70, 35, 74], [87, 65, 23], [70, 35, 74], [67, 85, 12], [45, 65, 0]] print("The list is:") print(data) K = 1 print("The value of K is:") print(K) result = [] ...
Read MorePython – Count frequency of sublist in given list
When it is required to count the frequency of a sub−list in a given list, a list comprehension and the 'len' method along with the 'if' condition are used. Example Below is a demonstration of the same − my_list = [23, 33, 45, 67, 54, 43, 33, 45, 67, 83, 33, 45, 67, 90, 0] print("The list is:") print(my_list) sub_list = [33, 45, 67] print("The sub-list is:") print(sub_list) my_result = len([sub_list for index in range(len(my_list)) if my_list[index : index + len(sub_list)] == sub_list]) print("The frequency count is:") print(my_result) The ...
Read MorePython – Segregate elements by delimiter
When we need to segregate elements based on a delimiter, Python provides several approaches using split() method with list comprehensions or loops. This is commonly used for processing structured data like CSV-style strings or log entries. Basic Approach Using List Comprehension The most efficient way is to use list comprehensions with the split() method ? data_list = ["89@21", "58@51", "19@61", "11@10", "32@65", "34@45", "87@90", "32@21", "1@345"] print("The original list is:") print(data_list) delimiter = "@" print(f"Using delimiter: {delimiter}") # Segregate elements before and after delimiter before_delim = [item.split(delimiter)[0] for item in data_list] after_delim = [item.split(delimiter)[1] ...
Read MorePython – Extract elements in between multiple specific range of index
When you need to extract elements from multiple specific index ranges in a list, you can use the extend() method with list slicing and iteration. This technique allows you to collect elements from different sections of a list based on defined ranges. Basic Example Here's how to extract elements from multiple index ranges ? my_list = [13, 21, 81, 10, 13, 17, 22, 18, 11, 90, 0] print("Original list:") print(my_list) # Define multiple ranges as (start, end) tuples range_list = [(2, 4), (7, 8), (1, 2)] # Extract elements from specified ranges result ...
Read MorePython – Sort List items on basis of their Digits
When sorting a list based on digit patterns rather than numerical value, we use string manipulation techniques. The sorted() method with lambda function and ljust() helps achieve lexicographic sorting of numbers. Example Below is a demonstration of sorting numbers based on their digit patterns ? my_list = [4344, 2611, 122, 541, 33, 892, 48292, 460, 390, 120, 10, 2909, 11239, 1] print("The list is:") print(my_list) print("The list after normal sorting is:") my_list_copy = sorted(my_list) print(my_list_copy) # Convert to strings to find max length my_temp_val = list(map(str, my_list)) my_max_length = max(map(len, my_temp_val)) # ...
Read MorePython – Random insertion of elements K times
When it is required to randomly insert elements K times, the random package provides methods like randint() and choice() to select random positions and elements for insertion. Example Below is a demonstration of the same − import random my_list = [34, 12, 21, 56, 8, 9, 0, 3, 41, 11, 90] print("The list is : ") print(my_list) print("The list after sorting is : ") my_list.sort() print(my_list) to_add_list = ["Python", "Object", "oriented", "language", "cool"] K = 3 print("The value of K is") print(K) for element in range(K): ...
Read MorePython program to find all the Combinations in a list with the given condition
When you need to find combinations in a list with a specific condition, you can use iteration, the yield operator, and list comprehension. This approach is useful for merging two lists by taking K elements from each list alternately. Syntax The basic approach involves creating a generator function that yields elements based on a given condition ? def merge_function(list1, list2, k): # Generator function logic yield element Example Below is a demonstration that merges two lists by alternating K elements from each ? ...
Read MorePython Pandas - Query the columns of a DataFrame
To query the columns of a Pandas DataFrame, use the query() method. This method allows you to filter records using a string expression, making it easier to write complex conditions in a readable format. Creating a DataFrame Let's start by creating a sample DataFrame with product inventory data ? import pandas as pd dataFrame = pd.DataFrame({ "Product": ["SmartTV", "PenDrive", "Speaker", "Earphone"], "Opening_Stock": [300, 700, 1200, 1500], "Closing_Stock": [200, 500, 1000, 900] }) print("DataFrame...") print(dataFrame) DataFrame... Product ...
Read MorePython Pandas - How to select multiple rows from a DataFrame
To select multiple rows from a DataFrame, you can use various methods including slicing with the : operator, loc[] for label-based selection, and iloc[] for position-based selection. Creating a DataFrame First, let's create a sample DataFrame to work with ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35], [40, 45]], index=['w', 'x', 'y', 'z'], ...
Read MorePython - How to select a column from a Pandas DataFrame
To select a column from a Pandas DataFrame, use square brackets with the column name. This returns a Series object containing all values from that column. Syntax dataFrame['ColumnName'] Basic Column Selection First, let's create a DataFrame and select a single column ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] } ) print("DataFrame ...") print(dataFrame) ...
Read More