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
Programming Articles
Page 342 of 2547
Python – 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 MoreMerge Python Pandas dataframe with a common column and set NaN for unmatched values
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 MorePython – Drop multiple levels from a multi-level column index in Pandas dataframe
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