TypeError : withcolumn() takes 3 positional arguments but 4 were given.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-21-2023 11:48 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2023 07:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 04:22 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-26-2023 07:25 AM
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

