![]() |
VOOZH | about |
Index.append() method in Pandas is used to concatenate or append one Index object with another Index or a list/tuple of Index objects, returning a new Index object. It does not modify the original Index. Example:
Index([1, 2, 3, 4, 5], dtype='int64')
Explanation: Two Pandas Index objects, idx1 and idx2, are created. The .append() method combines them into a new Index stored in res, confirming that both Index objects have been successfully merged.
Index.append(other, verify_integrity=False)
Parameters:
Returns: A new Index object with elements of other appended.
Example 1: In this example, three Index objects with string values are created and merged into one.
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
Explanation: Three Pandas Index objects, idx1, idx2 and idx3, are created with string values. The .append() method is used to merge idx2 and idx3 into idx1, resulting in a new combined Index.
Example 2: In this example, two Index objects with integer values are created and merged using the .append() method. Duplicates are allowed by default and retained in the result.
Output
Int64Index([10, 20, 20, 30], dtype='int64')Explanation: Two Pandas Index objects, idx1 and idx2, are merged using .append(), which allows duplicates by default. Since verify_integrity is no longer supported, values like 20 are retained without error.
Example 3: This example demonstrates how to manually prevent duplicate entries when merging Index objects.
Output
ValueError: Indexes have overlapping values: [2]Explanation: Two Pandas Index objects, idx1 and idx2, are merged using .append(). To prevent duplicates, a manual check using has_duplicates is performed and a ValueError is raised if any are found.