Server Side Programming Articles

Page 327 of 2109

Python - Compute last of group values in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 225 Views

The groupby().last() method in Pandas returns the last row of each group when data is grouped by one or more columns. This is useful for getting the most recent entry for each category in your dataset. Creating Sample Data First, let's create a DataFrame with some sample car sales data ? import pandas as pd dataFrame = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'BMW', 'Tesla', 'Lexus', 'Tesla'], "Place": ['Delhi', 'Bangalore', 'Pune', 'Punjab', 'Chandigarh', 'Mumbai'], "Units": [100, 150, 50, 80, 110, 90] }) print("Original DataFrame:") ...

Read More

Python Pandas - Filtering columns from a DataFrame on the basis of sum

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 951 Views

In Pandas, you can filter DataFrame columns based on their sum values using the loc[] indexer with conditional logic. This technique is useful when you want to select only columns that meet certain aggregate criteria. Creating the DataFrame First, let's create a sample DataFrame with student marks ? import pandas as pd # Create a DataFrame with student marks df = pd.DataFrame({ 'Jacob_Marks': [95, 90, 75, 85, 88], 'Ted_Marks': [60, 50, 65, 85, 70], 'Jamie_Marks': [77, 76, 65, 45, 50] }) print("Original ...

Read More

Python Pandas - Select first periods of time series data based on a date offset

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 249 Views

To select first periods of time series based on a date offset, use the first() method. This method filters rows from the beginning of a DataFrame up to a specified time period. Creating a Time Series DataFrame First, create a date range index with specific periods and frequency ? import pandas as pd # Create date index with 5 periods and frequency of 3 days i = pd.date_range('2021-07-15', periods=5, freq='3D') # Create DataFrame with date index dataFrame = pd.DataFrame({'k': [1, 2, 3, 4, 5]}, index=i) print("DataFrame...") print(dataFrame) DataFrame... ...

Read More

Python Pandas - Merge DataFrame with indicator value

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

To merge Pandas DataFrame with indicator information, use the merge() function with the indicator parameter set to True. This adds a special _merge column showing the source of each row. What is the Indicator Parameter? The indicator parameter creates a column that tracks whether each row comes from the left DataFrame, right DataFrame, or both ? import pandas as pd # Create DataFrame1 dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Mustang', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) ...

Read More

Python - Calculate the standard deviation of a column in a Pandas DataFrame

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 892 Views

Standard deviation measures how spread out values are from the mean. In Pandas, you can calculate the standard deviation of a DataFrame column using the std() method. Syntax To calculate standard deviation of a specific column ? dataframe['column_name'].std() Creating Sample DataFrames First, let's create sample DataFrames with numerical data ? import pandas as pd # Create DataFrame1 with car sales data dataFrame1 = pd.DataFrame({ "Car": ['BMW', 'Lexus', 'Audi', 'Tesla', 'Bentley', 'Jaguar'], "Units": [100, 150, 110, 80, 110, 90] }) print("DataFrame1:") print(dataFrame1) ...

Read More

Python Pandas - Select final periods of time series data based on a date offset

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 232 Views

To select final periods of time series based on a date offset, use the last() method. This method allows you to extract the most recent data points within a specified time window from a datetime-indexed DataFrame. Creating a Time Series DataFrame First, create a date range with specific periods and frequency ? import pandas as pd # Create date index with 5 periods and frequency of 3 days date_index = pd.date_range('2021-07-15', periods=5, freq='3D') print("Date Index:") print(date_index) Date Index: DatetimeIndex(['2021-07-15', '2021-07-18', '2021-07-21', '2021-07-24', ...

Read More

Python Pandas – Remove leading and trailing whitespace from more than one column

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

To remove leading and trailing whitespace from multiple columns in a Pandas DataFrame, use the str.strip() method. This is useful for cleaning data that contains unwanted spaces. Creating a DataFrame with Whitespace First, let's create a DataFrame with whitespace in the string columns ? import pandas as pd # Create a DataFrame with whitespace in string columns dataFrame = pd.DataFrame({ 'Product Category': [' Computer', ' Mobile Phone', 'Electronics ', 'Appliances', ' Furniture', 'Stationery'], 'Product Name': ['Keyboard', 'Charger', ' SmartTV', 'Refrigerators', ' Chairs', 'Diaries'], ...

Read More

Compare specific Timestamps for a Pandas DataFrame – Python

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 841 Views

To compare specific timestamps in a Pandas DataFrame, you can access individual rows using index numbers and calculate the difference between timestamp columns. This is useful for analyzing time intervals between related events. Creating a DataFrame with Timestamps First, let's create a DataFrame containing timestamp data ? import pandas as pd # Create a DataFrame with timestamp columns dataFrame = pd.DataFrame({ "Car": ["Audi", "Lexus", "Tesla", "Mercedes", "BMW"], "Date_of_Purchase": [ pd.Timestamp("2021-06-10"), pd.Timestamp("2021-07-11"), ...

Read More

Python Program to replace list elements within a range with a given number

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

When working with Python lists, you may need to replace multiple elements within a specific range with the same value. This can be efficiently accomplished using list slicing combined with list multiplication. Syntax The basic syntax for replacing elements within a range is ? list[start:end] = [new_value] * (end - start) Where: start − starting index (inclusive) end − ending index (exclusive) new_value − the value to replace with Basic Example Here's how to replace elements at indices 4 to 7 with the number 9 ? numbers = ...

Read More

Python Program to assign each list element value equal to its magnitude order

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 214 Views

When it is required to assign each list element value equal to its magnitude order, we can use the set operation, zip method and list comprehension. The magnitude order represents the rank of each element when sorted in ascending order. Understanding Magnitude Order Magnitude order assigns ranks based on sorted values. The smallest element gets rank 0, the next smallest gets rank 1, and so on. Example Below is a demonstration of assigning magnitude order to list elements ? my_list = [91, 42, 27, 39, 24, 45, 53] print("The list is :") print(my_list) ...

Read More
Showing 3261–3270 of 21,090 articles
« Prev 1 325 326 327 328 329 2109 Next »
Advertisements