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 327 of 2547
Python Pandas - How to select rows from a DataFrame by integer location
To select rows by integer location in a Pandas DataFrame, use the iloc accessor. The iloc method allows you to select rows and columns by their integer position, starting from 0. Creating a Sample DataFrame Let's start by creating a DataFrame to work with ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], ...
Read MorePython – Convert Suffix denomination to Values
When working with financial or numerical data, you often encounter values with suffix denominations like "5Cr" (5 Crores), "7M" (7 Million), or "30K" (30 Thousand). Python allows you to convert these suffix notations to their actual numerical values using dictionaries and string manipulation. Example Below is a demonstration of converting suffix denominations to values ? my_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"] print("The list is :") print(my_list) value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, ...
Read MorePython Pandas - How to select rows from a DataFrame by passing row label
To select rows by passing a label, use the loc[] accessor in Pandas. You can specify the index label to retrieve specific rows from a DataFrame. The loc[] accessor is label-based and allows you to select data by row and column labels. Creating a DataFrame with Custom Index Labels First, let's create a DataFrame with custom index labels ? import pandas as pd # Create DataFrame with custom index labels dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], ...
Read MorePython - Cast datatype of only a single column in a Pandas DataFrame
In Pandas, you can cast the datatype of only a single column using the astype()
Read MorePython – Check Similar elements in Matrix rows
When working with matrices, you might need to identify rows that appear multiple times. Python provides an efficient solution using the Counter class from the collections module to count the frequency of identical rows. Finding Similar Elements in Matrix Rows The approach involves converting matrix rows to tuples (since lists aren't hashable) and using Counter to track row frequencies ? from collections import Counter def find_duplicate_rows(matrix): # Convert rows to tuples for hashing matrix_tuples = map(tuple, matrix) # ...
Read MorePython – Check alternate peak elements in List
When it is required to check the alternate peak elements in a list, a function is defined that iterates through the list, compares adjacent elements, and returns the index of a peak element. A peak element is an element that is greater than or equal to its neighbors. Example Below is a demonstration of finding peak elements in a list − def find_peak(my_array, array_length): if (array_length == 1): return 0 if (my_array[0] >= my_array[1]): ...
Read MorePython – Extract Paired Rows
When working with lists of lists, we sometimes need to extract rows where every element appears an even number of times. This is called extracting "paired rows" since elements come in pairs. Python's list comprehension with the all() function provides an elegant solution. What are Paired Rows? A paired row is a list where every element appears an even number of times (2, 4, 6, etc.). For example, [30, 30, 51, 51] is paired because both 30 and 51 appear exactly twice. Syntax result = [row for row in list_of_lists if all(row.count(element) % 2 == ...
Read MorePython – All replacement combination from other list
When you need to generate all possible combinations by replacing elements from one list with elements from another, Python's itertools.combinations method provides an efficient solution. This technique creates combinations where some original elements are replaced with elements from a replacement list. Basic Example Here's how to generate replacement combinations using itertools.combinations − from itertools import combinations original_list = [54, 98, 11] print("Original list:") print(original_list) replace_list = [8, 10] print("Replacement list:") print(replace_list) # Combine both lists and generate combinations combined_list = original_list + replace_list result = list(combinations(combined_list, len(original_list))) print("All replacement combinations:") for combo ...
Read MorePython - Convert one datatype to another in a Pandas DataFrame
Use the astype() method in Pandas to convert one datatype to another in a DataFrame. This method allows you to convert columns to specific data types for memory optimization or compatibility requirements. Creating a DataFrame with Mixed Data Types Let's create a DataFrame with different column types ? import pandas as pd # Create DataFrame with mixed types dataFrame = pd.DataFrame({ "Reg_Price": [7000.5057, 1500, 5000, 8000, 9000.75768, 6000], "Units": [90, 120, 100, 150, 200, 130] }) print("DataFrame:") print(dataFrame) DataFrame: Reg_Price ...
Read MorePython - Sort rows by Frequency of K
When working with lists in Python, you may need to sort elements by their frequency of occurrence. This can be achieved using the Counter class from the collections module along with list comprehension. Understanding the Problem Sorting by frequency of 'K' means arranging elements so that the most frequently occurring items appear first, followed by less frequent ones. Elements with the same frequency maintain their relative order. Example Here's how to sort a list by element frequency using Counter.most_common() − from collections import Counter my_list = [34, 56, 78, 99, 99, 99, 99, ...
Read More