Found 10476 Articles for Python

Python Pandas - Return the seconds from Timedelta object

Arnab Chakraborty
Updated on 13-Oct-2021 07:57:49

231 Views

To return the seconds from Timedelta object, use the timedelta.seconds property. 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 objecttimedelta = pd.Timedelta('10 s 15 ms 33 ns') Display the Timedeltaprint("Timedelta...", timedelta)Return the seconds valuetimedelta.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('10 s 15 ms 33 ns') # display the Timedelta print("Timedelta...", timedelta) # return the seconds value res = timedelta.seconds ... Read More

Python Pandas - Replace index values where the condition is False

AmitDiwan
Updated on 13-Oct-2021 07:59:20

257 Views

To replace index values where the condition is False, use the index.isin() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name ='Products') Display the Pandas index −print("Pandas Index...", index)Replace values where condition is False. Here, except the 'Decor', every other element gets replaced −print("Replace index vales where condition is False...", index.where(index.isin(['Decor']), 'Miscellaneous'))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(['Electronics', 'Accessories', 'Decor', 'Books', 'Toys'], name ='Products') # Display the Pandas index print("Pandas Index...", index) # Return the ... Read More

Python Pandas - Return the microseconds from Timedelta object using string input

Arnab Chakraborty
Updated on 13-Oct-2021 07:55:42

206 Views

To return the nanoseconds from Timedelta object, use the timedelta.microseconds property. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Set string input for microseconds using unit 'us'. Create a Timedelta objecttimedelta = pd.Timedelta('12 min 40 us') Display the Timedeltaprint("Timedelta...", timedelta)Return the microseconds valuetimedelta.microseconds ExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # set string input for microseconds using unit 'us' # create a Timedelta object timedelta = pd.Timedelta('12 min 40 us') # Display the ... Read More

Python - Repeat each element of a Pandas Series in a dissimilar way

AmitDiwan
Updated on 13-Oct-2021 07:52:42

173 Views

To repeat each element of a Pandas Series in a dissimilar way, use the index.repeat() method. At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship'], name ='Transport') Display the Pandas index −print("Pandas Index...", index)Repeat each element of the index in a dissimilar way −print("Result after repeating each index element in a different way...", index.repeat([2, 3, 5, 7]))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship'], name ='Transport') # Display the Pandas index print("Pandas Index...", index) # Return the number ... Read More

Python Pandas - Return the microseconds from Timedelta object using integer input

Arnab Chakraborty
Updated on 13-Oct-2021 07:52:31

111 Views

To return the nanoseconds from Timedelta object, use the timedelta.microseconds property. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Set integer input for microseconds using unit 'us'. Create a Timedelta objecttimedelta = pd.Timedelta(55, unit ='us') Display the Timedeltaprint("Timedelta...", timedelta)Return the microseconds valuetimedelta.microseconds ExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # set integer input for microseconds using unit 'us' # create a Timedelta object timedelta = pd.Timedelta(55, unit ='us') # display the Timedelta print("Timedelta...", ... Read More

Python Pandas - Repeat elements of an Index

AmitDiwan
Updated on 13-Oct-2021 07:50:28

508 Views

To repeat elements of an Index, use the index.repeat() method in Pandas. Set the number of repetitions as an argument. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], name ='Transport') Display the Pandas index −print("Pandas Index...", index)Repeat elements of the index −print("Result after repeating each index element twice...", index.repeat(2)) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], name ='Transport') # Display the Pandas index print("Pandas Index...", index) # Return the number of elements ... Read More

Python Pandas - Return the nanoseconds from Timedelta object using string input

Arnab Chakraborty
Updated on 13-Oct-2021 07:48:23

126 Views

To return the nanoseconds from Timedelta object, use the timedelta.nanoseconds property. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Set string input for nanoseconds using unit 'ns'. Create a Timedelta object timedelta = pd.Timedelta('10 min 40 ns')Display the Timedeltaprint("Timedelta...", timedelta) Return the nanoseconds valuetimedelta.nanosecondsExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # set string input for nanoseconds using unit 'ns' # create a Timedelta object timedelta = pd.Timedelta('10 min 40 ns') # display the ... Read More

Python Pandas - Return the nanoseconds from Timedelta object using integer input

Arnab Chakraborty
Updated on 13-Oct-2021 07:46:51

321 Views

To return the nanoseconds from Timedelta object, use the timedelta.nanoseconds property. At first, import the required libraries −import pandas as pdTimeDeltas is Python’s standard datetime library uses a different representation timedelta’s. Set integer input for nanoseconds using unit 'ns'. Create a Timedelta objecttimedelta = pd.Timedelta(35, unit ='ns') Return the nanoseconds valuetimedelta.nanosecondsExampleFollowing is the code import pandas as pd # TimeDeltas is Python’s standard datetime library uses a different representation timedelta’s # set integer input for nanoseconds using unit 'ns' # create a Timedelta object timedelta = pd.Timedelta(35, unit ='ns') # display the Timedelta print("Timedelta...", timedelta) # return ... Read More

Python Pandas - Alter index name

AmitDiwan
Updated on 13-Oct-2021 07:46:52

226 Views

To alter index name, use the index.rename() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], name ='Transport') Display the Pandas index −print("Pandas Index...", index)Rename the index −print("Rename the index...", index.rename('Mode_of_Transport')) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Truck', 'Suburban'], name ='Transport') # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in the index...", index.size) # Return the dtype of ... Read More

Python Pandas - Return the nanoseconds from Timedelta object

Arnab Chakraborty
Updated on 13-Oct-2021 07:44:41

140 Views

To return the microseconds from Timedelta object, use the timedelta.nanoseconds property. 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 objecttimedelta = pd.Timedelta('4 days 10 min 25 s 15 ms 33 ns') Display the Timedeltaprint("Timedelta...", timedelta)Return the nanoseconds valuetimedelta.nanoseconds 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('4 days 10 min 25 s 15 ms 33 ns') # display the Timedelta print("Timedelta...", timedelta) # ... Read More

Advertisements