Found 10476 Articles for Python

Python Pandas - Convert Period to desired frequency

AmitDiwan
Updated on 20-Oct-2021 06:38:06

595 Views

To convert Period to desired frequency, use the period.asfreq() method. Let’s say we will set to desired Hourly frequency using the ‘H’ specifier.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Create two Period objectsperiod1 = pd.Period("2020-09-23 03:15:40") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Convert Period to desired frequency. We have set frequency as H i.e. Hourly frequencyres1 = period1.asfreq('H') res2 = period2.asfreq('H')ExampleFollowing is the code import pandas as pd # The pandas.Period represents a ... Read More

Python Pandas - Get the year from the Period object

AmitDiwan
Updated on 20-Oct-2021 06:34:31

541 Views

To get the year from the Period object, use the period.year property. At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objectsperiod1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Get the year from two Period objectsres1 = period1.year res2 = period2.yearExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month ... Read More

Python Pandas - Get the week of the year on the given Period

AmitDiwan
Updated on 20-Oct-2021 06:29:43

223 Views

To get the week of the year on the given Period, use the period.weekofyear property. At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objectsperiod1 = pd.Period("2020-09-23") period2 = pd.Period(freq="D", year = 2021, month = 4, day = 16, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Get the week of the year from two Period objectsres1 = period1.weekofyear res2 = period2.weekofyearExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2020-09-23") period2 ... Read More

Python Pandas - Find the start time for the given Period object

AmitDiwan
Updated on 20-Oct-2021 06:26:36

329 Views

To find the start time for the given Period object, use the period.start_time property. At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objectsperiod1 = pd.Period("2020-09-22") period2 = pd.Period(freq="D", year = 2021, month = 2, day = 14, hour = 2, minute = 35)Display the Period objectsprint("Period1...", period1) print("Period2...", period2)Get the start time from two Period objectsres1 = period1.start_time res2 = period2.start_timeExampleFollowing is the code import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2020-09-22") period2 = pd.Period(freq="D", year ... Read More

Program to find out the largest sum value of a BST in a given binary tree in Python

Arnab Chakraborty
Updated on 19-Oct-2021 12:43:31

210 Views

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 likethen the output will be 12.The BST in the given binary tree is −sum of the nodes = 12.To solve this, we will follow these steps −c := 0m := nullvalue := 0Define a function recurse() . This will take nodeif ... Read More

Program to find out if a BST is present in a given binary tree in Python

Arnab Chakraborty
Updated on 19-Oct-2021 12:29:28

170 Views

Suppose we are given a binary tree. We have to find out the largest subtree from the tree that is a binary search tree (BST). We return the root node of the BST.So, if the input is likethen the output will be −To solve this, we will follow these steps −c := 0m := nullDefine a function recurse() . This will take nodeif node is not null, thenleft_val := recurse(left of node)right_val := recurse(right of node)count := negative infinityif (node.left is same as null or node.left.val

Program to find out the k-th largest product of elements of two arrays in Python

Arnab Chakraborty
Updated on 19-Oct-2021 12:16:12

244 Views

Suppose we are given two lists, p and q that contain some integer numbers. We have to multiply all the values of these lists and have to find out the k-th largest value from the multiplication results.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. The 2nd largest element at is (index starts from 0) is 16.To solve this, we will follow these ... Read More

Program to find how many lines intersect in Python

Arnab Chakraborty
Updated on 19-Oct-2021 12:11:03

531 Views

Suppose we are given a list that contains values in pairs of (m, c). These values represent a line, where y = mx + c. We are also given two values, l, and r. We have to find out the number of the lines that intersect with each other between the range x = l to 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.If we look at the given photo, the lines 4x + 6 = 0 and -6x + ... Read More

Program to find out the total number of characters to be changed to fix a misspelled word in Python

Arnab Chakraborty
Updated on 19-Oct-2021 11:46:14

165 Views

Suppose we are given a list of cities and a list of roads that connect each other. The list 'cities' contain the name of cities that a tour bus visits in order. In the list 'roads' the roads are listed in a (source, destination) order meaning there is a one-way road from source to destination. Now, there is a problem that some city names in the list 'cities' may be misspelled. We have to correct such misspelled city names by changing the minimum number of characters. We return the number of characters changed as output.So, if the input is like ... Read More

Python Pandas - Create a Series from TimeDeltaIndex

AmitDiwan
Updated on 19-Oct-2021 11:10:11

176 Views

Use the to_series() method to create a series from TimeDeltaIndex in Pandas.At first, import the required libraries −import pandas as pdCreate a TimeDeltaIndex object. We have set the timedelta-like data using the 'data' parameter −tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])Convert TimeDeltaIndex to Series −print("TimeDeltaIndex to series...", tdIndex.to_series()) ExampleFollowing is the code −import pandas as pd # Create a TimeDeltaIndex object # We have set the timedelta-like data using the 'data' parameter tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999']) # ... Read More

Advertisements