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 244 of 2547
Python Pandas - Get the year from the Period object
To get the year from the Period object, use the period.year property. A pandas Period represents a specific span of time with a given frequency. Importing Required Library First, import pandas to work with Period objects ? import pandas as pd Creating Period Objects You can create Period objects using different approaches ? import pandas as pd # Create Period from date string period1 = pd.Period("2020-09-23") # Create Period with specific frequency and date components period2 = pd.Period(freq="D", year=2021, month=4, day=16, hour=2, minute=35) print("Period1:", period1) print("Period2:", period2) ...
Read MorePython Pandas - Get the week of the year on the given Period
To get the week of the year on the given Period, use the period.weekofyear property. This property returns the week number (1-53) for any given date within a Period object. Syntax period.weekofyear Where period is a pandas Period object representing a specific date or time period. Creating Period Objects First, import the required library and create Period objects using different methods − import pandas as pd # Creating Period objects using different approaches period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year=2021, month=4, day=16, hour=2, minute=35) # Display the Period objects ...
Read MorePython Pandas - Find the start time for the given Period object
To find the start time for a given Period object, use the start_time property. A Pandas Period represents a specific time span, and start_time returns the exact timestamp when that period begins. What is start_time Property? The start_time property returns a Timestamp object representing the start of the period. This is useful when you need the precise beginning moment of a time period ? import pandas as pd # Create a Period object period = pd.Period("2020-09-22") print("Period:", period) print("Start time:", period.start_time) print("Type:", type(period.start_time)) Period: 2020-09-22 Start time: 2020-09-22 00:00:00 Type: ...
Read MoreProgram to find out the largest sum value of a BST in a given binary tree in Python
Suppose we are provided a binary tree. We have to find out if there exist binary search trees (BST) in the subtrees of it and find out the sum of the largest BST. To find out the sum, we add the values of each node in that BST. We return the sum value as output. So, if the input is like ? then the output will be 12. The BST in the given binary tree is ? sum of the nodes = 12. Algorithm To solve this, we will follow ...
Read MoreProgram to find out if a BST is present in a given binary tree in Python
A Binary Search Tree (BST) is a special type of binary tree where the left subtree contains values less than or equal to the root, and the right subtree contains values greater than or equal to the root. This problem asks us to find the largest BST subtree within a given binary tree. Original Tree: 1 4 6 ...
Read MoreProgram to find out the k-th largest product of elements of two arrays in Python
Given two lists containing integer numbers, we need to find the k-th largest product by multiplying each element from the first list with each element from the second list. This problem can be efficiently solved using a min-heap to track the k largest products. So, if the input is like p = [2, 5], q = [6, 8], k = 2, then the output will be 16. The multiplication results are: 2 × 6 = 12, 2 × 8 = 16, 5 × 6 = 30, 5 × 8 = 40. When sorted in descending order: [40, 30, ...
Read MoreProgram to find how many lines intersect in Python
Sometimes we need to find how many lines intersect within a given range. Given a list of lines in the form (m, c) representing y = mx + c, we can determine which lines intersect between x = l and x = h. So, if the input is like input_list = [[4, 6], [-6, 10], [8, 12]], l = 0, h = 2, then the output will be 2. ...
Read MoreProgram to find out the total number of characters to be changed to fix a misspelled word in Python
This problem involves finding the minimum number of character changes needed to correct misspelled city names in a tour route. Given a list of cities that a bus visits in order and a list of one-way roads connecting cities, we need to find valid paths and calculate the minimum corrections required. Problem Understanding The algorithm uses dynamic programming to find the optimal path through valid roads while minimizing character differences between the given city names and actual city names. Algorithm Steps The solution follows these key steps − Create a helper function to calculate ...
Read MoreProgram to find out if a linked list is present in a given binary tree in Python
Suppose we are given a binary tree that has a root node 'root' and a linked list that has a head node 'head'. We have to find out if that linked list exists in that binary tree. If a set of nodes in the tree have links with each other in order as a linked list, and if that order is similar to that of the provided linked list, then we return 'True' or otherwise, we return 'False'. So, if the input is like ? 6 ...
Read MorePython Pandas - Return TimeDeltaIndex as object ndarray of datetime.datetime objects
To return TimeDeltaIndex as object ndarray of datetime.timedelta objects, use the TimeDeltaIndex.to_pytimedelta() method. This method converts Pandas timedelta objects to Python's native datetime.timedelta objects. Syntax TimedeltaIndex.to_pytimedelta() Creating a TimeDeltaIndex First, let's create a TimeDeltaIndex object with various timedelta formats − import pandas as pd # Create a TimeDeltaIndex object with different timedelta formats tdIndex = pd.TimedeltaIndex(data=['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', ...
Read More