Linsu Han
Linsu Han

Reputation: 165

When initiating a Pandas DataFrame, it automatically generates names of the columns like self.<column_name>; How?

Say I import a .csv file with columns [A, B, C] using df = pandas.read_csv()

Now I can access columns A, B, and C with df.A, df.B, df.C

However, defining object classes requires the variable to be predefined in their definition as in:

def Box:
    def __init__(self):
        self.A = 'ayy'
        self.B = 'bee'
        self.C = 'sea'

And they are accessed by calling Box.A, Box.B, Box.C

How do Pandas DataFrames make automatic generation of object attributes/properties?

Upvotes: 0

Views: 25

Answers (1)

Polor Beer
Polor Beer

Reputation: 2012

Pandas uses __getattr__ to create this quick access to columns.

Accessing data via df[..] is implemented by __getitem__.

Upvotes: 1

Related Questions