Server Side Programming Articles

Page 323 of 2109

How to delete a column from Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 1K+ Views

To delete a column from a Pandas DataFrame, you have several methods available: del statement, drop() method, and pop() method. Each approach has its own use cases and advantages. Using del Statement The del statement permanently removes a column from the DataFrame ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90], "Price": [50000, 60000, 45000, 40000, 80000, 70000] }) print("Original DataFrame:") print(dataFrame) # Delete a ...

Read More

Python – Sort Strings by Case difference

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 225 Views

When working with strings in Python, you may need to sort them based on the case difference — the absolute difference between uppercase and lowercase letters. This technique uses custom sorting with the sort() method and a key function that calculates case differences. Understanding Case Difference Case difference is calculated by counting uppercase and lowercase letters in each string, then finding their absolute difference. Strings with smaller case differences are considered "more balanced" and appear first when sorted. Example Here's how to sort strings by case difference using a custom function ? def get_diff(my_string): ...

Read More

Python – Filter Similar Case Strings

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 284 Views

When it is required to filter similar case strings, list comprehension can be used along with isupper() and islower() methods. This technique helps identify strings that are either completely uppercase or completely lowercase, filtering out mixed-case strings. Example Below is a demonstration of filtering strings with consistent casing ? my_list = ["Python", "good", "FOr", "few", "CODERS"] print("The list is :") print(my_list) my_result = [sub for sub in my_list if sub.islower() or sub.isupper()] print("The strings with same case are :") print(my_result) Output The list is : ['Python', 'good', 'FOr', 'few', ...

Read More

Python Pandas - How to select rows from a DataFrame by integer location

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 943 Views

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 More

Python – Convert Suffix denomination to Values

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 265 Views

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 More

Python Pandas - How to select rows from a DataFrame by passing row label

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 4K+ Views

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 More

Python - Cast datatype of only a single column in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 550 Views

In Pandas, you can cast the datatype of only a single column using the astype()

Read More

Python – Check Similar elements in Matrix rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 299 Views

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 More

Python – Check alternate peak elements in List

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 239 Views

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 More

Python – Extract Paired Rows

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 214 Views

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 More
Showing 3221–3230 of 21,090 articles
« Prev 1 321 322 323 324 325 2109 Next »
Advertisements