dataframeattributes

How to create class attribute as dataframe


I wanted to pass a dataset when creating an object so that I can write methods to operate on the object and modify the dataset, e.g.

`my_obj = study.Study(one_study, a, b)
`my_obj.addDate()

I tried the following code:

`import pandas as pd
`class Study(pd1.DataFrame):`
`def __init__(self, oneStudy, m_flag, s_flag):`
       `super().__init__(oneStudy) # Inherits DataFrame properties` 
       `self.stdyData = pd1.DataFrame(oneStudy, copy=True)`
       `self.stdyData.reset_index(inplace = False, drop = True)``

and it gave:

UserWarning: Pandas doesn't allow columns to be created via a new attribute name.

Also tried replacing self.stdyData = pd1.DataFrame(oneStudy, copy=True) with self.stdyData = oneStudy.copy() and got the same Pandas warning.

I tied the following as per replies in stack overflow on the same issue:

`import pandas as pd`
`class Study(pd1.DataFrame):`
`def __init__(oneStudy, m_flag, s_flag):`
       `super().__init__(oneStudy) # Inherits DataFrame properties`
       `self.stdyData = pd1.DataFrame(oneStudy, copy=True)`
       `self.stdyData.reset_index(inplace = False, drop = True)`

and the IDE complained self is not defined.

What should I do?


Solution

  • To answer your question this is just basic creating classes with attributes in python. There is no reason for inheritance here at all that I can see. Example:

    import pandas as pd
    
    class Study():
        def __init__(self, df):
            self.df = df
    
    mydf = pd.DataFrame({"Col1": [1,2,3]})
    study = Study(mydf)
    
    print(study.df.columns)