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 319 of 2547
Python Program to Get K initial powers of N
When it is required to get the specific number of powers of a number, the ** operator is used along with list comprehension to generate the first K powers of N. Syntax The basic syntax for calculating powers in Python ? # Using ** operator result = base ** exponent # Using list comprehension for multiple powers powers = [base ** i for i in range(k)] Example Below is a demonstration of getting K initial powers of N ? n = 4 k = 5 print("The value n is:", ...
Read MorePython Program to find Duplicate sets in list of sets
When working with lists of sets, you may need to identify which sets appear more than once. Python provides an efficient solution using Counter from the collections module and frozenset to handle duplicate sets. Why Use frozenset? Sets are mutable and unhashable, so they cannot be directly counted by Counter. frozenset creates an immutable, hashable version of a set that can be used as a dictionary key or counted. Example Below is a demonstration of finding duplicate sets ? from collections import Counter my_list = [{4, 8, 6, 1}, {6, 4, 1, 8}, ...
Read MoreAdd custom borders to a matrix in Python
When it is required to add custom borders to the matrix, a simple list iteration can be used to add the required borders to the matrix. This technique is useful for displaying matrices in a more readable format with visual boundaries. Example Below is a demonstration of the same ? my_list = [[2, 5, 5], [2, 7, 5], [4, 5, 1], [1, 6, 6]] print("The list is :") print(my_list) print("The resultant matrix is :") border = "|" for sub in my_list: my_temp = border + " " ...
Read MoreHow to append a list as a row to a Pandas DataFrame in Python?
To append a list as a row to a Pandas DataFrame in Python, we can use the concat() method (recommended) or the loc[] indexer. Let's first import the required library − import pandas as pd Following is the data in the form of lists of team rankings − Team = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ['New Zealand', 4 , 65], ['South Africa', 5, 50]] Creating a DataFrame with the above data and adding columns − import pandas as pd Team = [['India', 1, 100], ['Australia', ...
Read MorePython program to remove words that are common in two Strings
When working with text processing, you may need to remove words that are common between two strings and keep only the unique words. This can be achieved by counting word frequencies and filtering words that appear only once across both strings. Example Below is a demonstration of removing common words from two strings − def common_words_filter(string_1, string_2): word_count = {} # Count words from first string for word in string_1.split(): word_count[word] = word_count.get(word, 0) + ...
Read MorePython Program to Extract Elements from a List in a Set
When it is required to extract elements from a list that exist in a set, a simple 'for' loop with membership checking can be used. This technique filters the list to keep only elements that are present in the target set. Method 1: Using For Loop Below is a demonstration using a basic for loop with conditional checking − my_list = [5, 7, 2, 7, 2, 4, 9, 8, 8] print("The list is:") print(my_list) search_set = {6, 2, 8} print("The search set is:") print(search_set) my_result = [] for element in my_list: ...
Read MorePython Pandas - Filling missing column values with median
The median is a statistical measure that separates the higher half from the lower half of a dataset. In Pandas, you can fill missing values (NaN) in a DataFrame column with the median using the fillna() method combined with median(). Importing Required Libraries First, import Pandas and NumPy with their standard aliases ? import pandas as pd import numpy as np Creating DataFrame with Missing Values Create a DataFrame containing NaN values using np.NaN ? import pandas as pd import numpy as np # Create DataFrame with missing values dataFrame ...
Read MorePython program to mask a list using values from another list
When working with data analysis or filtering tasks, you often need to create a binary mask from one list based on values present in another list. Python provides several approaches to accomplish this task efficiently. What is List Masking? List masking creates a binary list (containing 0s and 1s) where 1 indicates the element exists in the reference list and 0 indicates it doesn't. This technique is commonly used in data filtering and boolean indexing. Using List Comprehension The most Pythonic approach uses list comprehension to create a mask ? my_list = [5, 6, ...
Read MoreHow to append a list to a Pandas DataFrame using loc in Python?
The DataFrame.loc accessor is used to access a group of rows and columns by label or a boolean array. We can use it to append a list as a new row to an existing DataFrame by specifying the next available index position. Creating the Initial DataFrame Let us first create a DataFrame with team ranking data ? import pandas as pd # Data in the form of list of team rankings team_data = [['India', 1, 100], ['Australia', 2, 85], ['England', 3, 75], ...
Read MorePython Pandas - Filling missing column values with mode
Mode is the value that appears most frequently in a dataset. In Pandas, you can fill missing values with the mode using the fillna() method combined with mode(). This is useful when you want to replace NaN values with the most common value in a column. Syntax dataframe.fillna(dataframe['column'].mode()[0], inplace=True) Creating DataFrame with Missing Values Let's start by importing the required libraries and creating a DataFrame with some missing values − import pandas as pd import numpy as np # Create DataFrame with NaN values dataFrame = pd.DataFrame({ ...
Read More