Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Programming Articles
Page 252 of 2547
Python Pandas - How to Round the DateTimeIndex with hourly frequency
To round the DateTimeIndex with hourly frequency, use the DateTimeIndex.round() method. For hourly frequency, use the freq parameter with value 'H'. Creating a DateTimeIndex At first, import the required libraries and create a DateTimeIndex with period 5 and frequency as 35 minutes − import pandas as pd # Create DatetimeIndex with period 5 and frequency as 35 minutes datetimeindex = pd.date_range('2021-09-29 07:00', periods=5, tz='Australia/Adelaide', freq='35T') # Display DateTimeIndex print("DateTimeIndex...", datetimeindex) DateTimeIndex... DatetimeIndex(['2021-09-29 07:00:00+09:30', '2021-09-29 07:35:00+09:30', '2021-09-29 ...
Read MoreProgram to find minimum costs needed to fill fruits in optimized way in Python
Suppose we have a list called fruits and two values k and cap. Each fruits[i] contains three values: [c, s, t], where fruit i costs c each, has size s, and there are t total fruits available. The k represents number of fruit baskets with capacity cap. We want to fill the fruit baskets with the following constraints in this order ? Each basket can only hold same type fruits Each basket should be as full as possible Each basket should be as cheap as possible ...
Read MorePython Pandas - Snap time stamps in DateTimeIndex to nearest occurring frequency
To snap time stamps in DateTimeIndex to nearest occurring frequency, use the DateTimeIndex.snap() method. This method rounds timestamps to the nearest frequency boundary, such as month-end, week-start, or any other valid frequency. Syntax DateTimeIndex.snap(freq) Parameters freq: A frequency string (e.g., 'M' for month-end, 'W' for week, 'D' for day) Basic Example Let's create a DateTimeIndex and snap timestamps to the nearest month-end ? import pandas as pd # Create DatetimeIndex with period 6 and frequency as D i.e. day # The timezone is Australia/Adelaide datetimeindex = pd.date_range('2021-10-20 ...
Read MoreProgram to find minimum cost to send same number of people to two different cities in Python
Suppose we have a list called costs, where costs[i] has [c1, c2] indicating that for person i it costs c1 amount to reach city 0 and c2 amount to reach city 1. We want the same number of people to go to city 0 as city 1, and we need to find the minimum cost required. So, if the input is like costs = [[2, 6], [10, 3], [4, 9], [5, 8]], then the output will be 17, because person 0 and 2 will go to city 0 and person 1 and 3 to city 1. For city 0, ...
Read MoreProgram to check first player can win by reaching total sum to target in Python
Suppose we have two numbers k and target. Now consider Amal and Bimal are playing a game. In each round, Amal picks a number from 1 to k to add to the total score that initially starts from 0. Whoever crosses the total to target wins. Amal always plays first, we have to check whether he can force a win if both of them play optimally. So, if the input is like k = 5, target = 10, then the output will be True, as if Amal picks 4 first, then whether Bimal picks 1, 2, ..., or 5, ...
Read MoreProgram to find indices or local peaks in Python
A local peak is an element that is greater than or equal to its neighbors. In Python, we can find indices of local peaks by comparing each element with its adjacent elements. A peak can be a single element or a plateau (consecutive equal elements that are peaks). Peak Definition An index i is a peak when these conditions are met: The next different number is either absent or smaller than nums[i] The previous different number is either absent or smaller than nums[i] There is at least one different number on either side Algorithm ...
Read MorePython Pandas - Return an Index of formatted strings specified by date format
The DateTimeIndex.strftime() method in Pandas returns an Index of formatted strings based on a specified date format. This method is useful for converting datetime objects into human-readable string representations. Syntax DateTimeIndex.strftime(date_format) Parameters: date_format − A string specifying the format using strftime directives Creating a DateTimeIndex First, let's create a DateTimeIndex with timezone information − import pandas as pd # Create DatetimeIndex with period 7 and frequency as 2 days datetimeindex = pd.date_range('2021-10-30 02:30:50', periods=7, tz='Australia/Adelaide', freq='2D') print("DateTimeIndex...") print(datetimeindex) DateTimeIndex... DatetimeIndex(['2021-10-30 02:30:50+10:30', '2021-11-01 02:30:50+10:30', ...
Read MorePython Pandas - Convert times to midnight in DateTimeIndex
To convert times to midnight in DateTimeIndex, use the DateTimeIndex.normalize() method in Pandas. This method sets the time component of all datetime values to 00:00:00 while preserving the date and timezone information. What is normalize()? The normalize() method converts the time component of datetime values to midnight (00:00:00). This is useful when you want to work with dates only, ignoring the time portion. Creating a DateTimeIndex First, let's create a DateTimeIndex with various times ? import pandas as pd # Create DateTimeIndex with period 7 and frequency as 10H (10 hours) # The ...
Read MorePython Pandas - Return index locations of values between particular time of day including start time in DateTimeIndex
To return index locations of values between particular time of day in DateTimeIndex, use the DateTimeIndex.indexer_between_time() method. Set the include_start parameter to True for including the start time. Syntax DateTimeIndex.indexer_between_time(start_time, end_time, include_start=True, include_end=True) Parameters Parameter Description start_time Start time as string in format 'HH:MM:SS' end_time End time as string in format 'HH:MM:SS' include_start Whether to include start time (default: True) include_end Whether to include end time (default: True) Example Let's create a DatetimeIndex and find index locations between specific times ...
Read MoreProgram to find sum of the minimums of each sublist from a list in Python
Suppose we have a list of numbers called nums. We have to find the sum of minimum values for every possible sublist in nums. If the answer is too large, then mod the result by 10^9 + 7. So, if the input is like nums = [5, 10, 20, 10, 0], then the output will be 90 because the sublists are [[5], [10], [20], [10], [0], [5, 10], [10, 20], [20, 10], [10, 0], [5, 10, 20], [10, 20, 10], [20, 10, 0], [5, 10, 20, 10], [10, 20, 10, 0], [5, 10, 20, 10, 0]], and their minimum ...
Read More