Apply class method to pandas dataframe column within other class method

Question:

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:

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.

Asked By: corvusMidnight

||

Answers:

You need call tweets_tokenizer method in Foo class with self

df['tokenized_comments'] =  df.iloc[:, c_column].apply(self.tweets_tokenizer)
Answered By: Ynjxsjmh
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.