Found 10476 Articles for Python

Python Pandas - Return Index with duplicate values removed except the first occurrence

AmitDiwan
Updated on 13-Oct-2021 11:33:32

171 Views

To return Index with duplicate values removed except the first occurrence, use the index.drop_duplicates() method. Use the keep parameter with value first.At first, import the required libraries −import pandas as pdCreating the index with some duplicates −index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane']) Display the index −print("Pandas Index with duplicates...", index)Return Index with duplicate values removed. The "keep" parameter with value "first" keeps the first occurrence for each set of duplicated entries −index.drop_duplicates(keep='first') ExampleFollowing is the code −import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane']) # Display the ... Read More

Python Pandas - Make new Index with passed list of labels deleted

AmitDiwan
Updated on 13-Oct-2021 11:31:13

341 Views

To make new Index with passed list of labels deleted, use the index.drop() method. Pass the list of labels in it.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane']) Display the index −print("Pandas Index...", index)A list containing labels to be dropped are passed −print("Updated index after deleting labels...", index.drop(['Bike', 'Ship'])) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index(['Car', 'Bike', 'Truck', 'Ship', 'Airplane']) # Display the index print("Pandas Index...", index) # Return the dtype of the data print("The dtype object...", index.dtype) ... Read More

Python - Make new Pandas Index with deleting multiple index elements

AmitDiwan
Updated on 13-Oct-2021 11:30:27

640 Views

To make new Pandas Index with deleting multiple index elements, use the index.delete() method. Set the multiple index elements in it.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 35, 45, 55, 75, 95])Display the index −print("Pandas Index...", index)Deleting multiple indexes at 3rd position i.e. index 2 and 5th position i.e. index 4 −print("Remaining Index after deleting multiple index elements...", index.delete([2, 4]))ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 35, 45, 55, 75, 95]) # Display the index print("Pandas Index...", index) # ... Read More

Python - Make new Pandas Index with passed location deleted

AmitDiwan
Updated on 13-Oct-2021 11:29:03

100 Views

To make new Pandas Index with passed location deleted, use the index.delete() method.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 35, 45, 55]) Display the index −print("Pandas Index...", index)Deleting a single index at 3rd position i.e. index 2 −print("Remaining Index after deleting an index at location 3rd (index 2)...", index.delete(2)) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 35, 45, 55]) # Display the index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in ... Read More

Python Pandas - Return the int position of the largest value in the Index

AmitDiwan
Updated on 13-Oct-2021 11:28:05

124 Views

To return the int position of the largest value in the Index, use the index.argmax() method.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55]) Display the index −print("Pandas Index...", index)Get the int position of the largest value in the Index −print("Get the int position of the largest value in the Index...", index.argmax()) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55]) # Display the index print("Pandas Index...", index) # ... Read More

Python Pandas - Return the int position of the smallest value in the Index

AmitDiwan
Updated on 13-Oct-2021 11:27:08

129 Views

To return the int position of the smallest value in the Index, use the index.argmin() method.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55]) Display the index −print("Pandas Index...", index)Get the int position of the smallest value in the Index −print("Get the int position of the smallest value in the Index...", index.argmin()) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 55, 10, 100, 70, 35, 40, 55]) # Display the index print("Pandas Index...", index) # ... Read More

Python Pandas - Return whether any element in the index is True

AmitDiwan
Updated on 13-Oct-2021 11:26:20

270 Views

To return whether all elements in the index are True, use the index.any() method in Pandas.At first, import the required libraries −import pandas as pdCreating the index with some True (non-zero) and False (zero) elements −index = pd.Index([15, 25, 0, 0, 55]) Display the index −print("Pandas Index...", index)Return True if any element in the index is True: −print("Check whether any element in the index is True...", index.any()) ExampleFollowing is the code −import pandas as pd # Creating the index with some True (non-zero) and False (zero) elements index = pd.Index([15, 25, 0, 0, 55]) # Display the index ... Read More

Python Pandas - Return whether all elements in the index are True

AmitDiwan
Updated on 13-Oct-2021 11:25:19

127 Views

To return whether all elements in the index are True, use the index.all() method in Pandas.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 35, 45, 55]) Display the index −print("Pandas Index...", index)Return True if all the elements in the index are True −print("Check whether all elements are True...", index.all()) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 35, 45, 55]) # Display the index print("Pandas Index...", index) # Return a tuple of the shape of the underlying data print("A tuple of ... Read More

Python Pandas - Return the memory usage of the Index values

AmitDiwan
Updated on 13-Oct-2021 11:24:32

180 Views

To return the memory usage of the Index values, use the index.memory_usage() method in Pandas.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([15, 25, 35, 45, 55]) Display the index −print("Pandas Index...", index)Get the memory usage of the values −print("The memory usage...", index.memory_usage()) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([15, 25, 35, 45, 55]) # Display the index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in the index...", index.size) # Return a tuple of ... Read More

Python Pandas - Check if the index is empty with 0 elements

AmitDiwan
Updated on 13-Oct-2021 11:23:38

2K+ Views

To check if the index is empty with 0 elements, use the index.empty property in Pandas.At first, import the required libraries −import pandas as pdCreating the index −index = pd.Index([]) Display the index −print("Pandas Index...", index)Check for empty index −print("Is the index empty?", index.empty) ExampleFollowing is the code −import pandas as pd # Creating the index index = pd.Index([]) # Display the index print("Pandas Index...", index) # Return the number of elements in the Index print("Number of elements in the index...", index.size) # check for empty index print("Is the index empty?", index.empty)OutputThis will produce the following code ... Read More

Advertisements