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
Server Side Programming Articles
Page 326 of 2109
Python Program to convert a list into matrix with size of each row increasing by a number
When it is required to convert a list into a matrix with the size of every row increasing by a number, the floor division operator // and list slicing are used together with iteration. Example Below is a demonstration of the same − my_list = [42, 45, 67, 89, 99, 10, 23, 12, 31, 43, 60, 1, 0] print("The list is :") print(my_list) my_key = 3 print("The value of key is") print(my_key) my_result = [] for index in range(0, len(my_list) // my_key): my_result.append(my_list[0: (index + 1) * my_key]) ...
Read MorePython program to sort tuples by frequency of their absolute difference
When it is required to sort tuples by frequency of their absolute difference, the lambda function, the abs method and the sorted method are used. This technique sorts tuples based on how often their absolute difference appears in the dataset. Understanding Absolute Difference The absolute difference between two numbers is the positive difference between them. For a tuple (a, b), the absolute difference is abs(a - b). Example Below is a demonstration of sorting tuples by frequency of their absolute difference ? my_list = [(11, 26), (21, 33), (90, 11), (26, 21), (32, 18), ...
Read MorePython Program to Remove First Diagonal Elements from a Square Matrix
When it is required to remove the first diagonal elements from a square matrix, the enumerate function and list comprehension can be used. The first diagonal (main diagonal) consists of elements where the row index equals the column index. Understanding the Main Diagonal In a square matrix, the main diagonal contains elements at positions (0, 0), (1, 1), (2, 2), and so on. These are the elements we need to remove ? Matrix with Main Diagonal ...
Read MorePython Program to Extract Strings with at least given number of characters from other list
When you need to extract strings that contain at least a specified number of characters from a given character list, list comprehension provides an efficient solution. This technique counts how many target characters appear in each string and filters based on a minimum threshold. Example Below is a demonstration of extracting strings with at least 2 characters from a specified character list ? my_list = ["Python", "is", "fun", "to", "learn"] print("The list is :") print(my_list) my_char_list = ['e', 't', 's', 'm', 'n'] my_key = 2 print("The value of key is") print(my_key) my_result ...
Read MorePython program to randomly create N Lists of K size
When you need to generate multiple random lists of a specific size from a larger dataset, Python's random.shuffle() combined with generator functions provides an efficient solution. This approach creates N lists, each containing K randomly selected elements. Using Generator Function with Shuffle Here's how to create a generator that produces random sublists ? from random import shuffle def gen_random_list(my_val, K): while True: shuffle(my_val) yield my_val[:K] my_list = [12, 45, 76, 32, 45, 88, 99, ...
Read MorePython - Count distinct in Pandas Aggregation with Numpy
To count distinct values in Pandas aggregation, use nunique() method. This tutorial shows how to group by a column, calculate sums using NumPy, and count distinct values simultaneously. Required Libraries Import the necessary libraries for data manipulation ? import pandas as pd import numpy as np Creating Sample DataFrame Create a DataFrame with duplicate values to demonstrate distinct counting ? import pandas as pd import numpy as np dataFrame = pd.DataFrame({ "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Lexus'], "Place": ['Delhi', 'Bangalore', 'Delhi', 'Chandigarh', ...
Read MorePython - Remove duplicate values from a Pandas DataFrame
To remove duplicate values from a Pandas DataFrame, use the drop_duplicates() method. This method identifies rows with identical values across all columns and removes the duplicates, keeping only the first occurrence of each unique row. Creating a DataFrame with Duplicates Let's create a sample DataFrame containing duplicate rows ? import pandas as pd # Create DataFrame with duplicate rows dataFrame = pd.DataFrame({ 'Car': ['BMW', 'Mercedes', 'Lamborghini', 'BMW', 'Mercedes', 'Porsche'], 'Place': ['Delhi', 'Hyderabad', 'Chandigarh', 'Delhi', 'Hyderabad', 'Mumbai'], 'UnitsSold': [95, 70, 80, 95, 70, 90] ...
Read MorePython – Group and calculate the sum of column values of a Pandas DataFrame
In Pandas, you can group DataFrame rows by specific columns and calculate aggregated values like sum, mean, or count. This is particularly useful for analyzing time-series data where you want to group by periods like months, quarters, or years. Creating a Sample DataFrame Let's create a DataFrame with car sales data to demonstrate grouping and summing ? import pandas as pd # Create DataFrame with car sales data dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW", "Toyota", "Nissan", "Bentley", "Mustang"], "Date_of_Purchase": [ ...
Read MoreWhat are the challenges faced by transport layer protocol?
In the OSI (Open System Interconnection) model, the transport layer is the fourth layer responsible for end-to-end communication between sender and receiver over the internet. It provides logical communication and ensures reliable delivery of data packets across the network. Transport Layer Protocols The main transport layer protocols include ? TCP (Transmission Control Protocol) − Connection-oriented, reliable protocol UDP (User Datagram Protocol) − Connectionless, fast protocol SCTP (Stream Control Transmission Protocol) − Message-oriented reliable protocol RDP (Reliable Data Protocol) − Reliable multicast protocol RUDP (Reliable User Datagram Protocol) − UDP with reliability features Responsibilities of ...
Read MorePython Pandas - Generate dates in a range
Pandas date_range() function generates a sequence of dates within a specified range. This is useful for creating time series data, filtering datasets by date periods, or setting up date indexes for analysis. Syntax pd.date_range(start, end, periods, freq) Parameters The key parameters are: start − Starting date of the range end − Ending date of the range periods − Number of dates to generate freq − Frequency (default is 'D' for daily) Generating Dates with ...
Read More