Python Articles

Page 294 of 855

Python Pandas - Create an Index based on an underlying Categorical

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 597 Views

To create an Index based on an underlying Categorical, use the pandas.CategoricalIndex() method. A CategoricalIndex can only take on a limited and usually fixed number of possible values, making it memory-efficient for repetitive data. Syntax pandas.CategoricalIndex(data=None, categories=None, ordered=None, dtype=None) Parameters The key parameters for creating a CategoricalIndex are ? data − Array-like values for the categorical index categories − List of possible values (categories) ordered − Boolean indicating if categories have a meaningful order dtype − Data type, typically 'category' Creating a Basic CategoricalIndex First, import pandas and create ...

Read More

Python Pandas - Display the value of the step parameter of RangeIndex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 356 Views

To display the value of the step parameter of RangeIndex, use the index.step property in Pandas. RangeIndex is a memory-efficient special case of Int64Index that represents monotonic ranges without storing all values in memory. What is RangeIndex? RangeIndex is optimized for representing evenly spaced integer sequences. It stores only the start, stop, and step values rather than all individual index values, making it memory-efficient for large ranges. Creating a RangeIndex First, let's create a RangeIndex with specific parameters ? import pandas as pd # Create a RangeIndex with start=10, stop=30, step=2 index = ...

Read More

Program to find minimum time required to complete tasks with k time gap between same type tasks in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 461 Views

Suppose we have a list of integers called tasks where each item represents a different task type, and a non-negative integer k. Each task takes one unit of time to complete and tasks must be completed in the given order, but we must have k units of time between executing two tasks of the same type. At any time, we can either execute a task or wait. We need to find the minimum time required to complete all tasks. For example, if tasks = [0, 1, 1, 2] and k = 2, the output will be 6. The first ...

Read More

Python Pandas - Display the value of the stop parameter of RangeIndex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 530 Views

To display the value of the stop parameter of RangeIndex, use the index.stop property in Pandas. RangeIndex is a memory-saving special case of Int64Index limited to representing monotonic ranges, which can improve computing speed in some instances. What is RangeIndex? RangeIndex represents a sequence of integers with a start, stop, and step value. It's similar to Python's range() function but optimized for pandas operations ? import pandas as pd # Create a RangeIndex with start=10, stop=30, step=2 index = pd.RangeIndex(start=10, stop=30, step=2, name="data") print("RangeIndex...") print(index) RangeIndex... RangeIndex(start=10, stop=30, step=2, name='data') ...

Read More

Python Pandas - Display the value of the start parameter of RangeIndex

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 275 Views

To display the value of the start parameter of RangeIndex, use the index.start property in Pandas. RangeIndex is a memory-saving special case of Int64Index limited to representing monotonic ranges that can improve computing performance. What is RangeIndex? RangeIndex represents a monotonic integer range similar to Python's range() function. It stores only the start, stop, and step values instead of the entire sequence, making it memory-efficient for large ranges. Creating a RangeIndex You can create a RangeIndex by specifying start, stop, step, and an optional name ? import pandas as pd # Create a ...

Read More

Program to find array by swapping consecutive index pairs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 577 Views

Given a list of numbers, we need to swap consecutive pairs at even indexes with each other, and consecutive pairs at odd indexes with each other. This creates a specific pattern where elements at positions (0, 2), (1, 3), (4, 6), (5, 7), etc. are swapped. Problem Understanding The swapping pattern works as follows ? Swap elements at indexes 0 and 2 (first even pair) Swap elements at indexes 1 and 3 (first odd pair) Swap elements at indexes 4 and 6 (second even pair) Swap elements at indexes 5 and 7 (second odd pair) Continue ...

Read More

Python Pandas - How to create a RangeIndex

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

A RangeIndex is a memory-efficient special case of Int64Index in Pandas that represents monotonic ranges. It's particularly useful for creating sequential indices without storing every individual value in memory. What is RangeIndex? RangeIndex is similar to Python's range() function but designed for Pandas indexing. It stores only the start, stop, and step values, making it memory-efficient for large sequential indices. Basic Syntax pd.RangeIndex(start=0, stop=None, step=1, name=None) Creating a Simple RangeIndex Here's how to create a basic RangeIndex with default parameters ? import pandas as pd # Create a simple ...

Read More

Python Pandas - Compute slice locations for input labels

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 175 Views

The slice_locs() method in Pandas allows you to compute slice locations for input labels in an Index. It returns a tuple containing the start and end positions that can be used for slicing operations. Syntax Index.slice_locs(start=None, end=None, step=None, kind=None) Parameters The method accepts the following parameters: start − Label to start the slice from (optional) end − Label to end the slice at (optional) step − Step size for slicing (optional) kind − Type of slicing behavior (optional) Basic Example Let's create an Index and find slice locations between ...

Read More

Program to check sum of two numbers is up to k from sorted List or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 713 Views

Suppose we have a list of numbers called nums and the elements in nums are sorted in ascending order. We also have another value k, we have to check whether any two elements taken from the list add up to k or not. The numbers can also be negative or 0. We have to solve this problem in constant amount of space usage. So, if the input is like nums = [-8, -3, 2, 7, 9] k = 4, then the output will be True, because if we take 7 and -3, then the sum is 7 + (-3) ...

Read More

Program to find sum of two numbers which are less than the target in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 822 Views

Suppose we have a list of numbers called nums and also have a target value, we have to find the sum of the largest pair of numbers in nums whose sum is at most (target-1). So, if the input is like nums = [8, 3, 4, 9, 2] target = 8, then the output will be 7, because the sum of the largest pair of numbers less than 8 is 4 + 3 = 7. Algorithm To solve this, we will follow these steps ? Sort the list nums Initialize p1 := 0 (left pointer) ...

Read More
Showing 2931–2940 of 8,546 articles
« Prev 1 292 293 294 295 296 855 Next »
Advertisements