Python - Checking whether dataframe is copy or view in pandas

Given two DataFrames, we need to check whether the DataFrames are copy or a view of some original DataFrame without any manipulation. By Pranit Sharma Last updated : September 27, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Difference between copy() and view() methods

The difference between copy() and view() is not a complex concept to understand. When we use copy, it makes a new copy of an array and any changes applied to the copied array will not make any impact on the original array. On the other hand, a view is a representation of the original array where if any changes are made to the view, it will make an impact on the original array or vice-versa.

Checking whether dataframe is copy or view in pandas

To check whether DataFrame is copy or view of another DataFrame, we will use DataFrame.is_view and DataFrame.is_copy method which is used to check whether the DataFrame is copy or view, in the case of view, it returns a Boolean value, and in the case of copy, it returns None if the DataFrame is not copy otherwise it returns the location of the DataFrame to which it is copied.

Let us understand with the help of an example,

Python program to check whether dataframe is copy or view in pandas

# Importing pandas package
import pandas as pd

# Creating a few DataFrames
df = pd.DataFrame(
    [[10,20,30,40],[50,60,70,80]], 
    index = ['One','Two'], 
    columns = ['A','B','C','D']
    )

df2 = df.iloc[0:4, :]

df3 = df.loc[df['A'] == 30, :]

# Display created DataFrames
print("Original DataFrame 1:\n",df,"\n")
print("Original DataFrame 2:\n",df2,"\n")
print("Original DataFrame 3:\n",df2,"\n")

# Checking DataFrames (view or copy)

# Checking df
print("Is DataFrame 1 a view ? \n",df._is_view,
    "\nIs DataFrame 1 a copy?\n",df._is_copy)

# Checking df2
print("Is DataFrame 2 a view ? \n",df2._is_view,
    "\nIs DataFrame 2 a copy?\n",df2._is_copy)

# Checking df3
print("Is DataFrame 3 a view ? \n",df3._is_view,
    "\nIs DataFrame 3 a copy?\n",df3._is_copy)

Output

The output of the above program is:

Example: Check dataframe is copy or view in pandas

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.