Change language

Python | Pandas Index.copy ()

Index.copy() Pandas Index.copy() makes a copy of this object. The function also sets the name and dtype of the new object as an attribute of the original object. If we want to have a different data type for the new object, we can do so by setting the dtype attribute of the function.

Syntax: Index.copy (name = None, deep = False, dtype = None, ** kwargs)

Parameters:
name: string, optional
deep : boolean, default False
dtype: numpy dtype or pandas type

Returns: copy: Index

Note: in most cases there should be no functional difference from using deep, but if deep is passed it will try to deep copy.

Example # 1: Use Index.copy () to copy the Index value into a new object and change the data type of the new object to int64.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ 17.3 , 69.221 , 33.1 , 15.5 , 19.3 , 74.8 , 10 , 5.5 ])

  
# Print index
idx

Output:

Let’s create a copy of an int64 object.

# Change data type again
# created an object in & # 39; int64 & # 39;

idx.copy (dtype = ’ int64’ )

Output:

As we see in the output, the function returned a copy of the original index with the int64 type.

Example # 2: Use Index.copy () to make a copy of the original object. Also set the name attribute of the new object and convert the string d to datetime.

# import pandas as pd

import pandas as pd

 
# Create index

idx = pd.Index ([ ’2015-10-31’ , ’2015-12-02’ , ’ 2016-01-03’

’2016-02-08’ , ’2017-05-05’ ])

 
# Print index
idx

Output:

Let’s make a copy of the original object.

# make a copy and set the data type in datetime format

idx_copy = idx.copy (dtype = ’datetime64’ )

  
# Print the newly created object
idx_copy

Output:
 
As we can see in the output, the new object has data in datatime format, and its name attribute has also been set.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically