JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Pandas Copy DataFrame

Author:JIYIK Last Updated:2025/05/01 Views:

This tutorial will show you how to DataFrame.copy()copy a DataFrame object using the copy method.

import pandas as pd

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708],
        "Cost": ["300", "400", "350"],
    }
)

print(items_df)

Output:

    Id Cost
0  302  300
1  504  400
2  708  350 

We will use the above example to demonstrate how to use the method in Pandas DataFrame.copy().


pandas.DataFrame.copy()Method Syntax

DataFrame.copy(deep=True)

It returns DataFramea copy of . deepBy default True, , which means any changes made in the copy will not be reflected in the original DataFrame. However, if we set deep=False, then any changes made in the copy will also be reflected in the original DataFrame.


pandas.DataFrame.copy()Copy a Pandas DataFrame using the

import pandas as pd
import numpy as np

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708],
        "Cost": ["300", "400", "350"],
    }
)

deep_copy = items_df.copy()

print("Original DataFrame before changing value in copy DataFrame:")
print(items_df, "\n")

print("Copy DataFrame before changing value in copy DataFrame:")
print(deep_copy, "\n")

deep_copy.loc[0, "Cost"] = np.nan

print("Original DataFrame after changing value in copy DataFrame:")
print(items_df, "\n")

print("Copy DataFrame after changing value in copy DataFrame:")
print(deep_copy, "\n")

Output:

Original DataFrame before changing value in copy DataFrame:
    Id Cost
0  302  300
1  504  400
2  708  350 

Copy DataFrame before changing value in copy DataFrame:
    Id Cost
0  302  300
1  504  400
2  708  350 

Original DataFrame after changing value in copy DataFrame:
    Id Cost
0  302  300
1  504  400
2  708  350 

Copy DataFrame after changing value in copy DataFrame:
    Id Cost
0  302  NaN
1  504  400
2  708  350 

It creates items_dfa copy of the DataFrame as deep_copy. If we change deep_copyany value of the copy, the original DataFrame items_dfremains unchanged. We set the value of the column deep_copyof the first row in to , but does not change.CostNaNitems_df


Assigning a Pandas DataFrame to a variable to copy the DataFrame

import pandas as pd
import numpy as np

items_df = pd.DataFrame(
    {
        "Id": [302, 504, 708],
        "Cost": ["300", "400", "350"],
    }
)

copy_cost = items_df["Cost"]

print("Cost column of Original DataFrame before changing value in copy DataFrame:")
print(items_df, "\n")

print("Cost column of Copied DataFrame before changing value in copy DataFrame:")
print(copy_cost, "\n")

copy_cost[0] = np.nan

print("Cost column of Original DataFrame after changing value in copy DataFrame:")
print(copy_cost, "\n")

print("Cost column of Copied DataFrame after changing value in copy DataFrame:")
print(copy_cost, "\n")

Output:

Cost column of Original DataFrame before changing value in copy DataFrame:
    Id Cost
0  302  300
1  504  400
2  708  350 

Cost column of Copied DataFrame before changing value in copy DataFrame:
0    300
1    400
2    350
Name: Cost, dtype: object 

Cost column of Original DataFrame after changing value in copy DataFrame:
0    NaN
1    400
2    350
Name: Cost, dtype: object 

Cost column of Copied DataFrame after changing value in copy DataFrame:
0    NaN
1    400
2    350
Name: Cost, dtype: object 

It creates columns items_dfin the DataFrame as .Costcopy_cost

Previous:Pandas DataFrame.ix[] Function

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Pandas DataFrame.ix[] Function

Publish Date:2025/05/01 Views:168 Category:Python

Python Pandas DataFrame.ix[] function slices rows or columns based on the value of the argument. pandas.DataFrame.ix[] grammar DataFrame . ix[index = None , label = None ] parameter index Integer or list of integers used to slice row indice

Pandas DataFrame.describe() Function

Publish Date:2025/05/01 Views:120 Category:Python

Python Pandas DataFrame.describe() function returns the statistics of a DataFrame. pandas.DataFrame.describe() grammar DataFrame . describe( percentiles = None , include = None , exclude = None , datetime_is_numeric = False ) parameter perc

Pandas DataFrame.astype() Function

Publish Date:2025/05/01 Views:160 Category:Python

Python Pandas DataFrame.astype() function changes the data type of an object to the specified data type. pandas.DataFrame.astype() grammar DataFrame . astype(dtype, copy = True , errors = "raise" ) parameter dtype The data type we want to a

Pandas DataFrame.to_dict() Function

Publish Date:2025/05/01 Views:188 Category:Python

Python Pandas DataFrame.to_dict() function converts the given DataFrame to a dictionary. pandas.DataFrame.to_dict() Syntax DataFrame . to_dict(orient = 'dict' , into = class ' dict ' ) parameter orient This parameter determines the type of

Pandas DataFrame.reset_index() Function

Publish Date:2025/05/01 Views:140 Category:Python

Python Pandas DataFrame.reset_index() function resets the index of the given DataFrame. It replaces the old index with the default index. If the given DataFrame has a MultiIndex, then this method removes all the levels. pandas.DataFrame.rep

Pandas DataFrame.resample() Function

Publish Date:2025/05/01 Views:78 Category:Python

Python Pandas DataFrame.resample() function resamples time series data. pandas.DataFrame.resample() Syntax DataFrame . resample( rule, axis = 0 , closed = None , label = None , convention = "start" , kind = None , loffset = None , base = No

Pandas DataFrame.insert() Function

Publish Date:2025/05/01 Views:116 Category:Python

Python Pandas DataFrame.insert() function inserts a column at the specified position into the DataFrame. pandas.DataFrame.insert() Syntax DataFrame . insert(loc, column, value, allow_duplicates = False ) parameter loc It is an integer param

Pandas DataFrame.idxmax() Function

Publish Date:2025/05/01 Views:79 Category:Python

Python Pandas DataFrame.idxmax() function returns the index of the maximum value in a row or column. pandas.DataFrame.idxmax() Syntax DataFrame . idxmax(axis = 0 , skipna = True ) parameter axis It is a parameter of integer or string type.

Pandas DataFrame sort_index() Function

Publish Date:2025/05/01 Views:183 Category:Python

This tutorial explains how to use pandas.DataFrame.sort_index() the sort method to sort a Pandas DataFrame based on its index. We will use the DataFrame shown in the above example to explain how to sort a Pandas DataFrame based on the index

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial