cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
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

4 REPLIES 4

Kaniz
Community Manager
Community Manager

Hi @JohnJustusThe error you're seeing is because you're trying to use the .withColumn function with more parameters than it accepts.

The withColumn function only takes three parameters: the name of the new column and a column expression. In your case, you're trying to create a new column, "Category", based on the length of the "Bin" column, but you're trying to do it for two different conditions in one go, causing the error.

Instead, it would be best if you used the when function with otherwise to handle multiple conditions. 

JohnJustus
New Contributor III

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
Valued Contributor II
Valued Contributor II

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

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.