Programming Articles - Page 963 of 3363

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

AmitDiwan
Updated on 19-Oct-2021 06:32:58

153 Views

To check Intervals that share closed endpoints overlap or not, use the IntervalArray.overlaps() method in Pandas.At first, import the required libraries −import pandas as pdTwo intervals overlap if they share a common point, including closed endpoints. Create an IntervalArrayintervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)]) Display the IntervalArray −print("IntervalArray...", intervals)Check Intervals that share closed endpoints overlap or not. We have set closed on the left-side with the "left" value of the "closed" parameter −print("Does interval that share closed endpoints overlap or not...", intervals.overlaps(pd.Interval(15, 28, closed='left')))ExampleFollowing is the code −import pandas as pd # Two intervals overlap if they share a ... Read More

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

AmitDiwan
Updated on 19-Oct-2021 06:28:00

358 Views

To check elementwise if an Interval overlaps the values in the IntervalArray, use the overlaps() method in Pandas.At first, import the required libraries −import pandas as pdTwo intervals overlap if they share a common point, including closed endpoints. Intervals that only have an open endpoint in common do not overlap. Create an IntervalArray −intervals = pd.arrays.IntervalArray.from_tuples([(10, 20), (15, 35)]) Display the IntervalArray −print("IntervalArray...", intervals)Check elementwise if an Interval overlaps the values in the IntervalArray −print("Does interval overlaps values in the IntervalArray...", intervals.overlaps(pd.Interval(12, 30)))ExampleFollowing is the code −import pandas as pd # Two intervals overlap if they share a common ... Read More

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

Arnab Chakraborty
Updated on 18-Oct-2021 13:28:47

342 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 have been walked. And when walk[i] is positive then indicates walked right, and negative for left. When we walk, we move one block, that is the next or previous integer position. We have to find the number of blocks that's been walked on at least target number of times.So, if the input is like walks = [3, -7, 2] target = 2, then the output will be 5, from the following figure, ... Read More

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

Arnab Chakraborty
Updated on 18-Oct-2021 13:26:26

200 Views

Suppose we have two binary matrices mat1 and mat2. Here 1 represents land and 0 represents water, if there is a group of 1(land) surrounded by water is called island. We have to find the number of islands that exist in both mat1 and mat2 at the exact same coordinates.So, if the input is like mat1 =101100100And mat2 =101100101then the output will be 2, because the overlapping islands are, 101100101so there are two overlapping islands.To solve this, we will follow these steps −r := row count of mat1c := column count of mat1last_row := r - 1last_col := c - ... Read More

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

Arnab Chakraborty
Updated on 18-Oct-2021 13:18:29

723 Views

Suppose we have a list of numbers called nums, they are sorted in ascending order, we also have another number target, we have to find the index where target should be inserted to keep nums sorted. If target already present in nums, then return the largest index where target can be inserted. We have to solve this without using library functions and solve it in O(log n) time.So, if the input is like nums = [1, 5, 6, 6, 8, 9] target = 6, then the output will be 4, because 6 is already there, so to insert it, the ... Read More

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

Arnab Chakraborty
Updated on 18-Oct-2021 13:15:56

178 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]s[i] < s[j]s[i] == s[j]So, if the input is like s = "pppxxp", then the output will be 1 because if we change the last "p" to "x", then this can satisfy the condition s[i] < s[j]To solve this, we will follow these steps −n := size ... Read More

Python Pandas - How to Round the DateTimeIndex with minute frequency

AmitDiwan
Updated on 18-Oct-2021 13:14:16

1K+ Views

To round the DateTimeIndex with minute frequency, use the DateTimeIndex.round() method. For minute frequency, use the freq parameter with value ‘T’.At first, import the required libraries −import pandas as pdDatetimeIndex with period 5 and frequency as s i.e. seconds. The 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)Round operation on DateTimeIndex date with minute frequency. For minute frequency, we have used 'T −print("Performing round operation with minute frequency...", datetimeindex.round(freq='T'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as s i.e. seconds # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 ... Read More

Python Pandas - How to Round the DateTimeIndex with hourly frequency

AmitDiwan
Updated on 18-Oct-2021 13:12:12

917 Views

To round the DateTimeIndex with hourly frequency, use the DateTimeIndex.round() method. For hourly frequency, use the freq parameter with value ‘H’.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 5 and frequency as T i.e. minutes −datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Round operation on DateTimeIndex date with hourly frequency. For hourly frequency, we have used 'H' −print("Performing round operation with hourly frequency...", datetimeindex.round(freq='H'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 5 and frequency as T i.e. minutes # timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, ... Read More

Program to find minimum costs needed to fill fruits in optimized way in Python

Arnab Chakraborty
Updated on 18-Oct-2021 13:13:11

2K+ Views

Suppose we have a list called fruits and another two values k and cap. Where each fruits[i] has three values: [c, s, t], this indicates fruit i costs c each, size of each of them is s, and there is total t of them. The k represents number of fruit baskets of capacity cap. We want to fill the fruit baskets with the following constraints in this order −Each basket can only hold same type fruitsEach basket should be as full as possibleEach basket should be as cheap as possibleSo we have to find the minimum cost required to fill ... Read More

Python Pandas - Snap time stamps in DateTimeIndex to nearest occurring frequency

AmitDiwan
Updated on 18-Oct-2021 13:09:06

242 Views

To snap time stamps in DateTimeIndex to nearest occurring frequency, use the DateTimeIndex.snap() method. Set the frequency using the freq parameter.At first, import the required libraries −import pandas as pdCreate a DatetimeIndex with period 6 and frequency as D i.e. day −datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') Display DateTimeIndex −print("DateTimeIndex...", datetimeindex)Snap time stamps to nearest occurring i.e. Month end here −print("Snap time stamps to nearest occurring frequency...", datetimeindex.snap(freq='M'))ExampleFollowing is the code −import pandas as pd # DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-20 02:30:50', periods=6, tz='Australia/Adelaide', freq='D') ... Read More

Advertisements