Found 507 Articles for Pandas

Python Pandas - Return Index with duplicate values completely removed

AmitDiwan
Updated on 13-Oct-2021 11:35:34

181 Views

To return Index with duplicate values completely removed, use the index.drop_duplicates() method.At first, import the required libraries −import pandas as pdCreating the indexwith 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 "False" drops all occurrences for each set of duplicated entries −print("Index with duplicate values removed (drops all occurrences)...", index.drop_duplicates(keep = False))ExampleFollowing is the code −import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car', 'Bike', 'Airplane', 'Ship', 'Airplane']) # Display the index print("Pandas Index ... Read More

Python Pandas - Return Index with duplicate values removed keeping the last occurrence

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

219 Views

To return Index with duplicate values removed keeping the last occurrence, use the index.drop_duplicates() method. Use the keep parameter with value last.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 "last" keeps the last occurrence for each set of duplicated entries −print("Index with duplicate values removed (keeping the last occurrence)...", index.drop_duplicates(keep='last')) ExampleFollowing is the code −import pandas as pd # Creating the index with some duplicates index = pd.Index(['Car', ... Read More

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

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

122 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

285 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 Pandas - Return the int position of the largest value in the Index

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

80 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

84 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

223 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

85 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

133 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