Found 10476 Articles for Python

Python Pandas - Return a Series containing counts of unique values from Index object sorted in Ascending Order

AmitDiwan
Updated on 13-Oct-2021 08:53:42

246 Views

To return a Series containing counts of unique values from Index object sorted in Ascending Order, use the index.value_counts() method with parameter ascending as True.At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Count of unique values sorted in ascending order −print("Get the count of unique values sorted in ascending order..." "", index.value_counts(ascending=True))ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the ... Read More

Python Pandas - Return a Series containing counts of unique values from Index object

AmitDiwan
Updated on 13-Oct-2021 08:51:00

318 Views

To return a Series containing counts of unique values from Index object, use the index.value_counts() method in Pandas.At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Count of unique values −print("Get the count of unique values...", index.value_counts()) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the Index ... Read More

Python Pandas - Return number of unique elements in the Index object

AmitDiwan
Updated on 13-Oct-2021 08:48:01

123 Views

To return number of unique elements in the Index object, use the index.nunique() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) Display the Pandas index −print("Pandas Index...", index)Get the number of unique values in the index −print("Count of unique values...", index.nunique()) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in ... Read More

Python Pandas - Return unique values in the index

AmitDiwan
Updated on 13-Oct-2021 08:45:34

4K+ Views

To return unique values in the index, use the index.unique() method in Pandas. At first, import the required libraries -import pandas as pdCreating Pandas index −index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30]) Display the Pandas index −print("Pandas Index...", index)Get the unique values from the index. Unique values are returned in order of appearance, this does NOT sort −index.unique() ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements ... Read More

Python Pandas - Mask and replace NaNs with a specific value

AmitDiwan
Updated on 13-Oct-2021 08:12:26

1K+ Views

To mask and replace NaNs with a specific value, use the index.putmask() method. Within that, set the index.isna() method.At first, import the required libraries -import pandas as pd import numpy as npCreating Pandas index with some NaNs −index = pd.Index([5, 65, 10, np.nan, 75, np.nan]) Display the Pandas index −print("Pandas Index...", index)Mask and replace NaN index values with a specific value −print("Mask...", index.putmask(index.isna(), 111)) ExampleFollowing is the code −import pandas as pd import numpy as np # Creating Pandas index with some NaNs index = pd.Index([5, 65, 10, np.nan, 75, np.nan]) # Display the Pandas index print("Pandas Index...", ... Read More

Python Pandas - Return a new Index of the values set with the mask

AmitDiwan
Updated on 13-Oct-2021 08:10:32

393 Views

To return a new Index of the values set with the mask, use the index.putmask() method in Pandas. At first, import the required libraries −import pandas as pdCreating Pandas index −index = pd.Index([5, 65, 10, 17, 75, 40]) Display the Pandas index −print("Pandas Index...", index)Mask and place index values less than 3 with a value 111 −print("Mask...", index.putmask(index < 30, 111)) ExampleFollowing is the code −import pandas as pd # Creating Pandas index index = pd.Index([5, 65, 10, 17, 75, 40]) # Display the Pandas index print("Pandas Index...", index) # Return the number of elements in the ... Read More

Python Pandas - Return a new Timedelta ceiled to this resolution

Arnab Chakraborty
Updated on 13-Oct-2021 08:09:21

81 Views

To return a new Timedelta ceiled to this resolution, use the timedelta.ceil() method. With that, set the resolution using the freq parameter.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('6 days 1 min 30 s') Display the Timedeltaprint("Timedelta...", timedelta)Return the ceiled Timestamp ceiled to days frequencyres = timedelta.ceil(freq='D') 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('6 days 1 min 30 s') # ... Read More

Python Pandas - Return a new Index of the values selected by the indices

AmitDiwan
Updated on 13-Oct-2021 08:07:57

131 Views

To return a new Index of the values selected by the indices, use the index.take() 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)Getting a new index of the values selected by indices −print("A new Index of the values selected by the indices...", index.take([1, 2]))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 ... Read More

Python Pandas - Get the seconds from Timedelta object using string input

Arnab Chakraborty
Updated on 13-Oct-2021 08:05:35

233 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. Set string input for seconds using unit 's'. Create a Timedelta objecttimedelta = pd.Timedelta('1 min 30 s') 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 # set string input for seconds using unit 's' # create a Timedelta object timedelta = pd.Timedelta('1 min 30 s') # display the ... Read More

Python Pandas - Get the seconds from Timedelta object using integer input

Arnab Chakraborty
Updated on 13-Oct-2021 08:02:20

275 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. Set integer input for seconds using unit 's'. Create a Timedelta objecttimedelta = pd.Timedelta(50, unit ='s') 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 # set integer input for seconds using unit 's' # create a Timedelta object timedelta = pd.Timedelta(50, unit ='s') # display the Timedelta print("Timedelta...", ... Read More

Advertisements