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 330 of 2547
Python 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 MorePython - Compute last of group values in a Pandas DataFrame
The groupby().last() method in Pandas returns the last row of each group when data is grouped by one or more columns. This is useful for getting the most recent entry for each category in your dataset. Creating Sample Data First, let's create a DataFrame with some sample car sales data ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] }) print("Original DataFrame:") ...
Read MorePython Pandas - Filtering columns from a DataFrame on the basis of sum
In Pandas, you can filter DataFrame columns based on their sum values using the loc[] indexer with conditional logic. This technique is useful when you want to select only columns that meet certain aggregate criteria. Creating the DataFrame First, let's create a sample DataFrame with student marks ? import pandas as pd # Create a DataFrame with student marks df = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], 'Jamie_Marks': [77, 76, 65, 45, 50] }) print("Original ...
Read MorePython Pandas - Select first periods of time series data based on a date offset
To select first periods of time series based on a date offset, use the first() method. This method filters rows from the beginning of a DataFrame up to a specified time period. Creating a Time Series DataFrame First, create a date range index with specific periods and frequency ? import pandas as pd # Create date index with 5 periods and frequency of 3 days i = pd.date_range('2021-07-15', periods=5, freq='3D') # Create DataFrame with date index dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) print("DataFrame...") print(dataFrame) DataFrame... ...
Read More