pandas.DataFrame.set_flags() Method with Examples

Learn about the Pandas DataFrame.set_flags() method, its usages, explanation, and examples. By Pranit Sharma Last updated : September 29, 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.

Python pandas.DataFrame.set_flags() Method

The pandas.DataFrame.set_flags() method returns a new object that is a view on a similar object as the input. If any changes are made to the input, the output values will be reflected in the other. This method is intended to be used in method chains. flags differ from metadata. flags reflect properties of the pandas object (the Series or DataFrame).

Syntax

The syntax of pandas.DataFrame.set_flags() method is:

DataFrame.set_flags(
    *, 
    copy=False, 
    allows_duplicate_labels=None
    )

Parameter(s)

The parameter(s) of pandas.DataFrame.set_flags() method is/are:

  • allows_duplicate_labels: The bool value which is optional, shows if return values have duplicates or not.

Python pandas.DataFrame.set_flags() Method Example

# Importing pandas package
import pandas as pd

# Creating two dictionaries
d1 = {'A':[i for i in range(100,500,100)]}

# Creating DataFrame
df = pd.DataFrame(d1)

# Display the DataFrame
print("Original DataFrame:\n",df,"\n")

# check set flags
print("Is duplicacy allowed?: ",df.flags.allows_duplicate_labels,"\n")

# Not allowing duplicates
df = df.set_flags(allows_duplicate_labels=False)

# display new flag
print("Is duplicacy allowed? :",df.flags.allows_duplicate_labels)

Output

The output of the above program is:

pandas.DataFrame.set_flags() Example Output

Reference: pandas.DataFrame.set_flags()

Python Pandas Programs »


Comments and Discussions!

Load comments ↻






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