0

I have a python class and it has functions like below:

class Features():
    def __init__(self, json):
        self.json = json

    def get_email(self):
        email = self.json.get('fields', {}).get('email', None)
        return email

And I am trying to use the get_email function in a pyspark dataframe to create a new column based on another column, "raw_json",which consists of json value:

df = data.withColumn('email', (F.udf(lambda j: Features.get_email(json.loads(j)), t.StringType()))('raw_json'))

So the ideal pyspark dataframe looks like below:

 +---------------+-----------
 |raw_json         |email
 +----------------+----------
 |                 |  
 +----------------+--------
 |                 |  
 +----------------+-------

But I am getting an error saying:

TypeError: unbound method get_email() must be called with Features instance as first argument (got dict instance instead)

How should I do to achieve this?

I have seen a similar question asked before but it was not resolved.

1 Answer 1

1

I guess you have misunderstood how classes are used in Python. You're probably looking for this instead:

udf = F.udf(lambda j: Features(json.loads(j)).get_email())
df = data.withColumn('email', udf('raw_json'))

where you instantiate a Features object and call the get_email method of the object.

Sign up to request clarification or add additional context in comments.

4 Comments

How can I apply the function on the "raw_json" column though..?
Have you tried Features = Features() first, then use data.withColumn('email', Features.get_email(col("raw_json"))?
@XXavier i got this error when doing Features = Features(): TypeError: __init__() takes exactly 2 arguments (1 given)
@kihhfeue I edited my answer to let you apply on the raw_json column. Your error of not defined parse_date is irrelevant to my suggestion - it's something else that is wrong in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.