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.

Related Posts

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

  • Find rows with nan in Pandas
    11 January

    Find rows with nan in Pandas

    Table of ContentsWhat is nan values in Pandas?Find rows with NAN in pandasFind columns with nan in pandasFind rows with nan in Pandas using isna and iloc() In this post, we will see how to find rows with nan in Pandas. What is nan values in Pandas? A pandas DataFrame can contain a large number […]

  • 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

    Pandas Loc Multiple Conditions

    💡 Outline Here is the code to select rows by pandas Loc multiple conditions. [crayon-627385a6b96cc645646401/] Here, we are select rows of DataFrame where age is greater than 18 and name is equal to Jay. [crayon-627385a6b96d4773883484/] The loc() function in a pandas module is used to access values from a DataFrame based on some labels. It […]

  • 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 […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.