Copy DataFrame in Pandas

Copy DataFrame in Python

💡 Outline
You can copy DataFrame in Pandas using copy() method of DataFrame. By default, it will create deep copy of DataFrame.

Output:

As visible in the output, the changes we made to the df1 DataFrame are reflected in the original df DataFrame. To create copies of a DataFrame, we can use the copy() function provided by the pandas module.

We have two different copies available in Python. A shallow copy and a deep copy.

A shallow copy creates a reference to the original object. Any changes made to this copy are affected in the original object as well.

However, a deep copy recursively adds all the data of the original object to a new object. This way any changes made in the deep copy are not reflected in the original copy.

## Using copy() method to deep copy DataFrame in Pandas
By default, the copy function creates a deep copy. It has a deep parameter, which is set to True by default.

For example,

Output:

In the above example, a deep copy of the original DataFrame was created. Changes made to this copy are not reflected in the original.

## Using copy() method to shallow copy DataFrame in Pandas
If we set the deep parameter as false, a shallow copy is created.

See the following code.

Output:

We can also use the earlier discussed assignment operator = to create shallow copies of DataFrames.

That’s all about how to copy DataFrame in Pandas.


import_contacts

You may also like:

Related Posts

  • 05 October

    Pandas replace values in column

    Table of ContentsUsing the loc() function to replace values in column of pandas DataFrameUsing the iloc() function to to replace values in column of pandas DataFrameUsing the map() function to replace values of a column in a pandas DataFrameUsing the replace() function to replace values in column of pandas DataFrameUsing the where() function to replace […]

  • Select rows by multiple conditions using loc in Pandas
    29 July

    Select rows by multiple conditions using loc in Pandas

    The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It returns the rows and columns which match the labels. We can use this function to extract rows from a DataFrame based on some conditions also. First, let us understand what happens when we provide a […]

  • Split dataframe in Pandas
    28 July

    Split dataframe in Pandas

    Table of ContentsUsing the iloc() function to split DataFrame in PythonBy RowsBy ColumnsUsing the sample() function to split DataFrame in PythonUsing the groupby() function to split DataFrame in PythonUsing the columns to split DataFrame in Python In real-life scenarios, we deal with massive datasets with many rows and columns. At times, we may want to […]

  • Read text file in Pandas
    28 July

    Read text file in Pandas

    Table of ContentsUsing the read_csv() function to read text files in PandasUsing the read_table() function to read text files in PandasUsing the read_fwf() function to read text files in Pandas A dataset has the data neatly arranged in rows and columns. The pandas module in Python allows us to load DataFrames from external files and […]

  • Pandas convert column to int
    18 June

    Pandas convert column to int

    Table of ContentsUse the to_numeric() function to convert column to intUse the astype() function to convert column to intUse the infer_objects() function to convert column to intUse the convert_dtypes() function to convert column to int Pandas is a library set up on top of the Python programming language and is mostly used for the purpose […]

  • 20 September

    Reorder the columns of pandas dataframe in Python

    Table of ContentsUsing reindex methodUsing column selection through column nameUsing column selection through column index In this post, we will see 3 different methods to Reordering the columns of Pandas Dataframe : Using reindex method You can use DataFrame’s reindex() method to reorder columns of pandas DataFrame. You need to pass columns=[$list_of_columns] to reindex() method […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.