cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

TypeError : withcolumn() takes 3 positional arguments but 4 were given.

JohnJustus
New Contributor III

Hi All,

Can some one please help me with the error.

This is my small python code.

binmaster = binmasterdf.withColumnRenamed("tag_number","BinKey")\

.withColumn ("Category", when (length("Bin")==4,'SMALL LOT'),(length("Bin")==7,'RACKING'))

TypeError : withcolumn() takes 3 positional arguments but 4 were given.

If I remove this (length("Bin")==7,'RACKING'), there are no issues.

Can some one please let me know how to fix the error?

Thanks,

John

3 REPLIES 3

Thanks for your response

Actually, here is my code with when function but I still see the error

binmaster = binmasterdf.withColumnRenamed("tag_number","BinKey")\

.withColumn ("Category", when (length("Bin")==4,'SMALL LOT'), when (length("Bin")==7,'RACKING'), otherwise('FLOOR'))

Is there anything wrong with the above?

Thanks,

John

 

Noopur_Nigam
Databricks Employee
Databricks Employee

Hi @JohnJustus If you see closely in .withColumn ("Category", when (length("Bin")==4,'SMALL LOT'), when (length("Bin")==7,'RACKING'), otherwise('FLOOR')), withcolumn would take 2 parameters. The first parameter as a string and the second as the column. However in your code, you are passing 3 parameters i.e. first is string and the other two are when conditions so the last when condition is being considered as an extra parameter in withcolumn function. This is the reason removing last when is resolving the issue.

Thank you Noopur for your response.

So how can I accomplish with more than 2 parameters? Are there any other function available?

In this case, I have to check length multiple times and categorize them accordingly.

Thanks,

John

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you wonโ€™t want to miss the chance to attend and share knowledge.

If there isnโ€™t a group near you, start one and help create a community that brings people together.

Request a New Group