Programming Articles

Page 251 of 2547

Python Pandas - Get the Names of levels in MultiIndex

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

To get the names of levels in MultiIndex, use the MultiIndex.names property in Pandas. A MultiIndex is a multi-level, or hierarchical, index object that allows you to work with higher dimensional data in a lower dimensional form. Creating a MultiIndex First, let's create a MultiIndex using arrays. The names parameter sets the names for each index level ? import pandas as pd # Create arrays for MultiIndex arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']] # Create MultiIndex with named levels multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student')) # Display the ...

Read More

Python Pandas IntervalIndex - Return an ndarray of tuples of the form (left, right)

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 340 Views

To return an ndarray of tuples of the form (left, right), use the to_tuples() method in Pandas. This method converts an IntervalArray or IntervalIndex into an array of tuples representing the interval boundaries. Creating an IntervalArray First, let's create an IntervalArray using from_breaks() ? import pandas as pd # Create IntervalArray from range breaks index = pd.arrays.IntervalArray.from_breaks(range(5)) print("IntervalArray...") print(index) IntervalArray... [(0, 1], (1, 2], (2, 3], (3, 4]] Length: 4, dtype: interval[int64, right] Converting to Tuples Use the to_tuples() method to get an ndarray of tuples ? ...

Read More

Python Pandas IntervalArray - Check Intervals that only have an open endpoint in common overlap or not

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 171 Views

To check if intervals that only have an open endpoint in common overlap or not, use the overlaps() method with the closed parameter in Pandas IntervalArray. Understanding Interval Overlap Two intervals overlap if they share a common point, including closed endpoints. However, intervals with only an open endpoint in common (like (10, 20) and (20, 30) where 20 is open in the first interval) may or may not overlap depending on the closed parameter. Creating an IntervalArray First, let's create an IntervalArray with intervals that share an endpoint ? import pandas as pd ...

Read More

Python Pandas IntervalArray - Check Intervals that share closed endpoints overlap or not

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 181 Views

To check if intervals that share closed endpoints overlap or not, use the IntervalArray.overlaps() method in Pandas. Two intervals overlap if they share a common point, including closed endpoints. Syntax IntervalArray.overlaps(other) Where other is an Interval object to check for overlaps against each interval in the array. Creating an IntervalArray First, let's create an IntervalArray with some sample intervals − import pandas as pd # Create an IntervalArray with two intervals intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)]) print("IntervalArray...") print(intervals) print("Interval length...") print(intervals.length) IntervalArray... [(10, 20], ...

Read More

Check elementwise if an Interval overlaps the values in the IntervalArray in Python Pandas

AmitDiwan
AmitDiwan
Updated on 26-Mar-2026 386 Views

To check elementwise if an Interval overlaps the values in the IntervalArray, use the overlaps() method in Pandas. Two intervals overlap if they share a common point, including closed endpoints. Intervals that only have an open endpoint in common do not overlap. Syntax IntervalArray.overlaps(other) Parameters other − An Interval object to check overlap against each interval in the array. Creating an IntervalArray First, let's create an IntervalArray from tuples − import pandas as pd # Create an IntervalArray intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)]) # Display the IntervalArray ...

Read More

Program to count how many blocks are covered k times by walking in Python

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

Suppose we have two lists called walks and target. At the beginning we are at position 0 in a one-dimensional line. Now |walks[i]| represents the number of steps we have walked. When walks[i] is positive it indicates we walked right, and negative for left. When we walk, we move one block at a time to the next or previous integer position. We have to find the number of blocks that have been walked on at least target number of times. So, if the input is like walks = [3, -7, 2] and target = 2, then the output will ...

Read More

Program to count number of overlapping islands in two maps in Python

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

Suppose we have two binary matrices mat1 and mat2. Here 1 represents land and 0 represents water. If there is a group of 1s (land) surrounded by water, it's called an island. We need to find the number of islands that exist in both mat1 and mat2 at the exact same coordinates. Problem Understanding Given two matrices, we need to count overlapping islands where both matrices have land (1) at the same positions. For example, if mat1 = 101 100 100 And mat2 = 101 100 101 ...

Read More

Program to find index, where we can insert element to keep list sorted in Python

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

Finding the correct insertion index to maintain sorted order is a common problem that can be efficiently solved using binary search. We need to find the rightmost position where we can insert the target element while keeping the list sorted in ascending order. Given a sorted list nums and a target value, we want to find the index where the target should be inserted. If the target already exists, we return the largest possible index (after all existing occurrences). Problem Example For nums = [1, 5, 6, 6, 8, 9] and target = 6, the output should ...

Read More

Program to find how many updates required to make string half monotonous in Python

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

Suppose we have a lowercase string s whose length is even. We have to find the minimum number of characters that need to be updated such that one of the following three conditions is satisfied for all i, where 0 ≤ i < n/2 and j, n/2 ≤ j < n − s[i] > s[j] (left half characters greater than right half) s[i] < s[j] (left half characters less than right half) s[i] == s[j] (left half characters equal to right half) So, if the input is ...

Read More

Python Pandas - How to Round the DateTimeIndex with minute frequency

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

To round the DateTimeIndex with minute frequency, use the DateTimeIndex.round() method. For minute frequency, use the freq parameter with value 'T'. Creating a DateTimeIndex First, let's create a DateTimeIndex with seconds frequency to demonstrate the rounding operation ? import pandas as pd # DatetimeIndex with period 5 and frequency as 45 seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='45s') # display DateTimeIndex print("DateTimeIndex...", datetimeindex) print("DateTimeIndex frequency...", datetimeindex.freq) The output of the above code is ? DateTimeIndex... DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:00:45+09:30', ...

Read More
Showing 2501–2510 of 25,466 articles
« Prev 1 249 250 251 252 253 2547 Next »
Advertisements