Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Apply class method to pandas dataframe column within other class method

I am sure there are other questions which provide an answer to mine, but I could not find them. So please, if you are aware of them, just redirect me to those.

I have created a class object:

class Foo:

    def __init__(self, file_path: str, language = None):

        self.language = 'italian' if language is None else language


        # Assig to self object
        self.file_path = file_path
        self.file_type = file_path[-3:]

    def tweets_tokenizer(self, text):
     
        language = data_manager
        txt = word_tokenize(txt, language=self.language)
    
        return txt
        
    def get_dictionary(self):

        
        
        df = self.load() #I have a class method that loads the df, which I did not include in the 
                         #code here
        c_column = int(input(f'What is the index of the column containing the comments?'))
        comments = df.iloc[:, c_column]

        df['tokenized_comments'] =  df.iloc[:, c_column].apply(Foo.tweets_tokenizer)

      
        output = df.to_dict('index')
        

        return output

When I call:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

item = Foo('filepath')
d = item.get_dictionary()

I get the following error:

TypeError: tweets_tokenizer() missing 1 required positional argument: 'text'

Which is directly related to

df['tokenized_comments'] =  df.iloc[:, c_column].apply(Foo.tweets_tokenizer)

Note that I have other static methods in the class which I can apply successfully without any issue. However, the Foo.tweet_tokenize method cannot be made static as I need to pass the language argument.

>Solution :

You need call tweets_tokenizer method in Foo class with self

df['tokenized_comments'] =  df.iloc[:, c_column].apply(self.tweets_tokenizer)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading