Server Side Programming Articles

Page 317 of 2109

Python Pandas - Sort DataFrame in ascending order according to the element frequency

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 929 Views

When working with DataFrames, you may need to sort data based on how frequently elements appear. This can be achieved by combining groupby(), count(), and sort_values() methods. Syntax To sort DataFrame in ascending order according to element frequency ? df.groupby(['column']).count().reset_index(name='Count').sort_values(['Count'], ascending=True) Creating the DataFrame First, let's create a DataFrame with car data ? import pandas as pd # Create DataFrame dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Mustang', 'Mercedes', 'Lexus'], "Reg_Price": [7000, 1500, 5000, 8000, 9000, 2000], "Place": ...

Read More

Python - Calculate the maximum of column values of a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 616 Views

To find the maximum value in Pandas DataFrame columns, use the max() function. This method works on individual columns or across the entire DataFrame to identify the highest values. Basic DataFrame Creation First, let's create a sample DataFrame with car data ? import pandas as pd # Create DataFrame with car data car_data = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("Car DataFrame:") print(car_data) Car DataFrame: Car ...

Read More

How to display Pandas Dataframe in Python without Index?

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

In Pandas, DataFrames display with an index by default. You can hide the index when displaying using to_string(index=False) or when printing to console using various methods. Creating a Sample DataFrame Let's start by creating a DataFrame with custom index and column labels ? import pandas as pd # Create DataFrame with custom index dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]], index=['x', 'y', 'z'], ...

Read More

Python - Generate all possible permutations of words in a Sentence

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 932 Views

When it is required to generate all possible permutations of words in a sentence, Python's itertools.permutations() function provides an efficient solution. This function takes a list of words and returns all possible arrangements. Using itertools.permutations() The permutations() function from the itertools module generates all possible orderings of the input elements ? from itertools import permutations def calculate_permutations(my_string): word_list = list(my_string.split()) permutes = permutations(word_list) for perm in permutes: permute_list = list(perm) ...

Read More

Python program to extract rows from Matrix that has distinct data types

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 249 Views

When working with matrices containing mixed data types, you may need to extract rows where each element has a distinct data type. This can be achieved by comparing the number of unique types in a row with the total number of elements. Example Below is a demonstration of extracting rows with distinct data types − my_list = [[4, 2, 6], ["python", 2, {6: 2}], [3, 1, "fun"], [9, (4, 3)]] print("The list is:") print(my_list) my_result = [] for sub in my_list: # Get unique data types in the current row ...

Read More

Python - Split list into all possible tuple pairs

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 492 Views

When working with lists, you might need to generate all possible ways to split the list into tuple pairs. This involves creating partitions where elements can either remain as individual items or be grouped into tuples with other elements. Example Below is a demonstration of splitting a list into all possible tuple pair combinations ? def determine_pairings(my_list): if len(my_list)

Read More

Python program to Reverse a range in list

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 246 Views

When it is required to reverse a specific range of elements in a list, Python provides several approaches. We can use slicing with the [::-1] operator to reverse a portion of the list while keeping other elements in their original positions. Method 1: Using Slicing to Reverse a Range We can reverse elements between specific indices using slicing ? my_list = [1, 2, 3, 4, 5, 6, 7, 8] print("Original list:") print(my_list) # Reverse elements from index 2 to 5 start, end = 2, 5 my_list[start:end+1] = my_list[start:end+1][::-1] print("List after reversing range [2:6]:") print(my_list) ...

Read More

Python - Cumulative Mean of Dictionary keys

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 377 Views

When working with a list of dictionaries, you may need to calculate the cumulative mean of values for each key that appears across all dictionaries. This involves collecting all values for each unique key and computing their average. Example Below is a demonstration of calculating cumulative mean across multiple dictionaries ? from statistics import mean my_list = [{'hi': 24, 'there': 81, 'how': 11}, {'hi': 16, 'how': 78, 'doing': 63}] print("The list is:") print(my_list) my_result = dict() for sub in my_list: ...

Read More

Python - How to fill NAN values with mean in Pandas?

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

When working with datasets in Pandas, missing values (NaN) are common. You can fill these NaN values with the mean of the column using mean() and fillna() functions. Creating a DataFrame with NaN Values Let us first import the required libraries and create a sample DataFrame ? import pandas as pd import numpy as np # Create DataFrame with NaN values dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Lexus', 'Mustang', 'Bentley', 'Mustang'], "Units": [100, 150, np.NaN, 80, np.NaN, np.NaN] }) print("Original DataFrame:") print(dataFrame) Original ...

Read More

Python - Given a list of integers that represents a decimal value, increment the last element by 1

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 537 Views

When working with a list of integers that represents a decimal value, we sometimes need to increment it by 1. This is similar to adding 1 to a number, but we need to handle carry operations when digits are 9. Problem Understanding Given a list like [1, 2, 3] representing the number 123, we want to increment it to get [1, 2, 4] representing 124. If the last digit is 9, we need to handle carries properly. Using Recursive Approach Here's a recursive solution that handles the carry operation when incrementing ? def increment_num(digits, ...

Read More
Showing 3161–3170 of 21,090 articles
« Prev 1 315 316 317 318 319 2109 Next »
Advertisements