Found 10476 Articles for Python

Python Pandas - Form the intersection of two Index objects and sort the result

AmitDiwan
Updated on 14-Oct-2021 06:52:58

668 Views

To form the intersection of two Index objects, use the index1.intersection(index2) method in Pandas. To sort the result, use the sort parameter.At first, import the required libraries −import pandas as pdCreating Pandas index1 and index2 −index1 = pd.Index([4, 3, 2, 1]) index2 = pd.Index([8, 2, 6, 4])Display the Pandas index1 and index2print("Pandas Index1...", index1) print("Pandas Index2...", index2)Perform intersection. The results are sorted using the "sort" parameterres = index1.intersection(index2, sort=None) ExampleFollowing is the code −import pandas as pd # Creating Pandas index1 and index2 index1 = pd.Index([4, 3, 2, 1]) index2 = pd.Index([8, 2, 6, 4]) # Display the ... Read More

Python Pandas - Get the Day of the year from Period object

AmitDiwan
Updated on 14-Oct-2021 06:52:27

202 Views

To get day of the year from Period object, use the period.dayofyear property.At first, import the required libraries −import pandas as pdThe 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 = 7, day = 16, hour = 2, minute = 35)Display the Period objects −print("Period1...", period1) print("Period2...", period2)Get the day of the year from two Period objects −res1 = period1.dayofyear res2 = period2.dayofyearExampleFollowing 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 - Get the Day of the week the period lies in

AmitDiwan
Updated on 14-Oct-2021 06:51:33

177 Views

To get day of the week that a Period falls on, use the period.dayofweek propertyAt first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objects −period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)Display the Period objects −print("Period1...", period1) print("Period2...", period2)Get the day of week from two Period objects −res1 = period1.dayofweek res2 = period2.dayofweekExampleFollowing is the code −import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = ... Read More

Python Pandas - Form the intersection of two Index objects

AmitDiwan
Updated on 14-Oct-2021 06:50:43

229 Views

To form the intersection of two Index objects, use the index1.intersection(index2) method in Pandas. At first, import the required libraries −import pandas as pdCreating two Pandas index −index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55])Display the Pandas index1 and index2 −print("Pandas Index1...", index1) print("Pandas Index2...", index2)Perform intersection −res = index1.intersection(index2) ExampleFollowing is the code −import pandas as pd # Creating two Pandas index index1 = pd.Index([10, 20, 30, 40, 50]) index2 = pd.Index([80, 65, 60, 70, 55]) # Display the Pandas index1 and index2 print("Pandas Index1...", index1) print("Pandas Index2...", index2) # ... Read More

Python Pandas - Get day of the month that a Period falls on

AmitDiwan
Updated on 14-Oct-2021 06:49:36

206 Views

To get day of the month that a Period falls on, use the period.day property.At first, import the required libraries −import pandas as pdThe pandas.Period represents a period of time. Creating two Period objects−period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, month = 9, day = 22, hour = 4, minute = 55)Get the day of the month from two Period objects −res1 = period1.day res2 = period2.dayExampleFollowing is the code −import pandas as pd # The pandas.Period represents a period of time # creating two Period objects period1 = pd.Period("2021-09-18") period2 = pd.Period(freq ='D', year = 2021, ... Read More

Python Pandas - Append a collection of Index options together

AmitDiwan
Updated on 14-Oct-2021 06:48:32

467 Views

To append a collection of Index options together, use the append() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index1 = pd.Index([10, 20, 30, 40, 50]) Display the Pandas index −print("Pandas Index...", index1)Create a new index to be appended −index2 = pd.Index([60, 70, 80]) Append the new index −print("After appending...", index1.append(index2))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index1 = pd.Index([10, 20, 30, 40, 50]) # Display the Pandas index print("Pandas Index...", index1) # Return the number of elements in the Index print("Number of elements in ... Read More

Python Pandas - Get the Total seconds in the duration from the Timedelta object

AmitDiwan
Updated on 14-Oct-2021 06:46:33

16K+ Views

To get the Total seconds in the duration from the Timedelta object, use the timedelta.total_seconds() method.At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s, Create a Timedelta object −timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') Display the Timedelta −print("Timedelta...", timedelta)Get the total seconds −timedelta.total_seconds() ExampleFollowing is the code −import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms ... Read More

Python Pandas - Sort index values and also return the indices that would sort the index

AmitDiwan
Updated on 14-Oct-2021 06:45:16

263 Views

To sort index values and also return the indices that would sort the index, use the index.sort_values(). The return_indexer parameter is set to be True.At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 95, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Sort index values. By default, sorts in Ascending order. Return the indices to sort the index using the "return_indexer" parameter with value True −print("Sort and also return the indices that would sort the index...", index.sort_values(return_indexer=True))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = ... Read More

Python Pandas - Convert the Timedelta to a NumPy timedelta64

AmitDiwan
Updated on 14-Oct-2021 06:44:12

2K+ Views

To convert the Timedelta to a NumPy timedelta64, use the timedelta.to_timedelta64() method.At first, import the required libraries −import pandas as pdCreate a Timedelta object −timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') Display the Timedelta −print("Timedelta...", timedelta)Convert the Timedelta to a NumPy timedelta64 −timedelta.to_timedelta64() ExampleFollowing is the code −import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') # display the Timedelta print("Timedelta...", timedelta) # Convert ... Read More

Python Pandas - Return a numpy.timedelta64 object with ns precision

AmitDiwan
Updated on 14-Oct-2021 06:41:05

388 Views

To return a numpy.timedelta64 object with ns precision, use the timedelta.to_timedelta64() method.At first, import the required libraries −import pandas as pdCreate a Timedelta object −timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') Display the Timedelta −print("Timedelta...", timedelta)Return a numpy.timedelta64 object with nanoseconds precision −timedelta.to_timedelta64() ExampleFollowing is the code −import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # create a Timedelta object timedelta = pd.Timedelta('2 days 11 hours 22 min 25 s 50 ms 45 ns') # display the Timedelta print("Timedelta...", timedelta) # ... Read More

Advertisements