Lambda () got an unexpected keyword argument ‘axis’

This article will explain what the <lambda>() got an unexpected keyword argument ‘axis’ means, what causes it, and how to resolve it. The <lambda>() function plays an important role in Python as it allows the creation of the functions.

It is claimed that lambda is an anonymous function. Although this function accepts several parameters, it always results in a single expression. Let us look and try to understand the cause of the <lambda>() got an unexpected keyword argument ‘axis’ and the ways to rectify it.

What is <lambda> function in Python?

It is also called an anonymous and small function. However, there are instances where it may result in an error message saying “keyword argument ‘axis'” when attempting to pass arguments to vectorized functions that involve lambda expressions.

You have noticed that when you get this error, it starts with a word called TypeError: <lambda> () got an unexpected keyword argument ‘axis’ error. TypeError is a built-in exception in Python programming language. It is raised when an operation or any function is pertained to a wrong type.

What causes <lambda>() got an unexpected keyword argument ‘axis’ error?

The syntax of the lambda function is quite restricted. It only allows for expression arguments. It does not support keyword arguments such as the axis. The syntax of a simple lambda function is as follows: lambda argumentsexpression.

The issue occurs when we attempt to provide arguments to functions that include lambda expressions. As a result, an error regarding a keyword argument is raised. Let us see through an example:

import pandas as pd
data = pd.DataFrame([[3, 8]] * 4, columns=['X', 'Y'])
data.apply(lambda x: 4, axis=1)

The above code snippet will return the following.

#Returns 
0    4
1    4
2    4
3    4
dtype: int64

As you can see, everything is fine up till now. The code has also returned the required output. But check out the line below. You can understand from here how the code throws <lambda> () got an unexpected keyword argument ‘axis’ error.

import pandas as pd
data = pd.DataFrame([[3, 8]] * 4, columns=['X', 'Y'])
data['X'].apply(lambda x: 4, axis=1)

The above code snippet will raise the following error.

TypeError: <lambda>() got an unexpected keyword argument 'axis

Here, you can see the same error hurled. The reason why this problem arises is because we have used a function directly to a Series. A series can be defined as a single column holding any kind and type of data, probably called a one-dimensional array.

What can be done to resolve <lambda>() got an unexpected keyword argument ‘axis’ error?

If we talk about the illustration that we have used above, then in this case, the simplest way to resolve this error is by removing the axis. As you can see here, using the axis parameter is trivial. So, if you encounter a similar problem, you can use this trick.

data['X'].apply(lambda x: 4)

The illustration must have helped you all to understand the causes and solution of the problem, resulting in <lambda>() got an unexpected keyword argument ‘axis error. As you can see, here we have used apply(), but if you are beginning our coding journey, then it is fair enough for you to use this method.

The limitation of using apply() is that this function is sometimes rather slow and will result in another loop, increasing the time for execution. If you are using a single column, this method is well enough. Let’s look at another example without using the apply() method.

data = pd.DataFrame([[3, 8]] * 2, columns=['X', 'Y'])
data['X'] = 1
print(data)

Here, DataFrame from pandas is used to accommodate two-dimensional data. Dataframe can be applied on a single row or multiple rows. One more function apart from apply() is assign(), which can be used with DataFrame from pandas. The assign() function can be applied to single or multiple columns.

FAQs

Are there any other options to use instead of lambda expressions in Python?

Yes, there are. We can define several compact functions. List comprehensions and Regular functions are the best choices instead of lambda expressions in Python.

What makes <lambda> a useful function?

Being an anonymous function, it is said that using the lambda function with another function is better. By providing enough memory to your system while writing codes, the function will reduce the time taken during execution, hence will increase the overall speed of the system.

Can multiple arguments use the lambda function?

Yes, we can use lambda with multiple arguments. As stated earlier, lambda function may utilize a variety of arguments, but the point is that it should only have a single expression.

Conclusion

We learned about the causes and solution of <lambda>() got an unexpected keyword argument ‘axis’ error. As stated earlier, the syntax of the lambda function is quite restricted, resulting in the error of <lambda> () got an unexpected keyword argument ‘axis’. We can resolve this problem by encapsulating lambdas in functions or allocating them to variables. One should thoroughly understand the lambda function functionality when using different vectorized functions to write reliable Python codes.

References

  1. <lambda>()
  2. pandas.Series

To learn more about fixes for common mistakes when writing in Python, follow us at Python Clear.

Leave a Comment