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 332 of 2547
Python – Strip whitespace from a Pandas DataFrame
To strip whitespace from a Pandas DataFrame, use the str.strip() method on string columns. This removes both leading and trailing whitespace characters from text data. Importing Pandas First, let us import the required Pandas library with an alias − import pandas as pd Creating a DataFrame with Whitespace Let's create a DataFrame with 3 columns where the first column has leading and trailing whitespaces − import pandas as pd dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], ...
Read MorePython program to compute the power by Index element in List
When it is required to compute the power by index element in a list, we can use iteration along with the ** operator to raise each element to the power of its index position. Understanding the Concept In this operation, each element at index i is raised to the power i. For example, element at index 0 becomes element ** 0 = 1, element at index 1 becomes element ** 1 = element, and so on. Using enumerate() with List Iteration The most straightforward approach uses enumerate() to get both index and element ? ...
Read MorePython - Filter dictionaries by values in Kth Key in list
When working with a list of dictionaries, you might need to filter them based on whether a specific key's value appears in a given list. This can be accomplished using simple iteration or more Pythonic approaches like list comprehension. Using For Loop Method The basic approach iterates through each dictionary and checks if the target key's value exists in the search list ? data_list = [{"Python": 2, "is": 4, "cool": 11}, {"Python": 5, "is": 1, "cool": 1}, ...
Read MorePython - Most common Combination in Matrix
When it is required to find the most common combination in a matrix, a simple iteration, along with the sort() method and Counter method is used. Example Below is a demonstration of the same − from collections import Counter from itertools import combinations my_list = [[31, 25, 77, 82], [96, 15, 23, 32]] print("The list is :") print(my_list) my_result = Counter() for elem in my_list: if len(elem) < 2: continue elem.sort() ...
Read MoreHow to Groupby values count on the Pandas DataFrame
To perform groupby value counts in Pandas, use the groupby(), size(), and unstack() methods. This technique helps you count occurrences of grouped data and reshape the results into a cross-tabulation format. Creating the DataFrame First, let's create a sample DataFrame with product information ? import pandas as pd # Create a DataFrame with 3 columns dataFrame = pd.DataFrame({ 'Product Category': ['Computer', 'Mobile Phone', 'Electronics', 'Electronics', 'Computer', 'Mobile Phone'], 'Product Name': ['Keyboard', 'Charger', 'SmartTV', 'Camera', 'Graphic Card', 'Earphone'], 'Quantity': [10, 50, 10, 20, 25, ...
Read MorePython - Unique values count of each Key
When it is required to find unique values count of every key, we can use several approaches including iteration with the 'append' method, the set() function, or dictionary-based counting methods. Using Iteration with Append Method This approach manually tracks unique values by checking if each element already exists in a filtered list ? my_list = [12, 33, 33, 54, 84, 16, 16, 16, 58] print("The list is :") print(my_list) filtered_list = [] elem_count = 0 for item in my_list: if item not in filtered_list: ...
Read MorePython program to Mark duplicate elements in string
When it is required to mark duplicate elements in a string, list comprehension along with the count method is used. This technique helps identify repeated elements by appending occurrence numbers to duplicates. Example Below is a demonstration of the same − my_list = ["python", "is", "fun", "python", "is", "fun", "python", "fun"] print("The list is :") print(my_list) my_result = [value + str(my_list[:index].count(value) + 1) if my_list.count(value) > 1 else value for index, value in enumerate(my_list)] print("The result is :") print(my_result) Output The list is : ['python', 'is', 'fun', 'python', 'is', ...
Read MorePython - Calculate the count of column values of a Pandas DataFrame
To calculate the count of column values in a Pandas DataFrame, use the count() method. This method returns the number of non-null values in each column, making it useful for data validation and analysis. Importing Required Library First, import the Pandas library − import pandas as pd Counting Values in a Specific Column You can count non-null values in a specific column by accessing the column and applying count() − import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame( { ...
Read MorePython - Index Directory of Elements
When it is required to index directory of elements in a list, list comprehension along with set operator is used. This creates a dictionary mapping each unique element to all its index positions in the list. Syntax {key: [index for index, value in enumerate(list) if value == key] for key in set(list)} Example Below is a demonstration of creating an index directory ? my_list = [81, 36, 42, 57, 68, 12, 26, 26, 38] print("The list is :") print(my_list) my_result = {key: [index for index, value in enumerate(my_list) ...
Read MorePython - Convert List to custom overlapping nested list
When it is required to convert a list to a customized overlapping nested list, an iteration along with the 'append' method can be used. This technique creates sublists of a specified size with a defined step interval, allowing elements to overlap between consecutive sublists. Syntax for index in range(0, len(list), step): result.append(list[index: index + size]) Parameters The overlapping nested list conversion uses two key parameters: step − The interval between starting positions of consecutive sublists size − The length of each sublist Example Below is ...
Read More