0

I have following code

display(ch2m_cpaf.groupby('PROVIDING_COMPANY_CODE').apply(lambda df: df, axis=0))

If I will run it with axis=0 argument, it raises

TypeError: () got an unexpected keyword argument 'axis'

But why axis=0 argument is not part of lambda expression?

I tried putting it in parenthesis, it should just return Pandas' Series object

4
  • always put full error message because there are other useful information. Commented Feb 28 at 1:42
  • better show minimal working code which we could simpy copy and run for tests. Commented Feb 28 at 1:43
  • I found that apply() for groupby() don't use axis=0 - you have to remove it. df.groupby().apply() works in different way than df.apply() - and it gets different parameters. Commented Feb 28 at 1:47
  • axis works only if I use df.groupby(...).apply(lambda data:data.apply(..., axis=0)) Commented Feb 28 at 1:53

1 Answer 1

1

Unlike the .apply() method of a DataFrame, which can take an axis argument, groupby().apply() applies fuctions group-wise and doesn't require an axis argument.

To fix the issue, simply remove axis=0;

display(ch2m_cpaf.groupby('PROVIDING_COMPANY_CODE').apply(lambda df: df))

If you want to perform operations on rows or columns, you should do it inside the lambda function;

display(ch2m_cpaf.groupby('PROVIDING_COMPANY_CODE').apply(lambda df: df.sum(axis=0)))  # Sum across columns
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.