1

I wrote a lambda function that should be fast, but this is taking a very long time. Is there a better way to write this?

fn = lambda x: shape(df[df.CustomerCard_Num == x.CustomerCard_Num])[0]
df['tottrans'] = df.apply(fn, axis = 1)

Basically, I have a big database of transactions (rows). A set of rows might correspond to different customers (Customer card number if a column in df, multiple rows might have the same df.CustomerCard_Num.)

I am trying to count the number of rows for each customer with this lambda function. But it does not seem to work quickly. Should I be using groupby?

8
  • 1
    just do df.CustomerCard_Num.value_counts() Commented Aug 29, 2014 at 19:49
  • As a side note, why did you write this with lambda instead of def (it's not anonymous, it's not being used in the middle of an expression, it's not transient…)? And, given that you tagged the question as lambda, it seems like you think it might even be relevant to your problem that you used lambda here. (It's not, but if you think it might be, why not write it the more idiomatic way and see?) Commented Aug 29, 2014 at 19:51
  • Also, why is this tagged with r? Commented Aug 29, 2014 at 19:51
  • 1
    Up to you but it does raise an interesting point about some common misunderstandings about lambdas and what they are for. Even if it was a straightforward function definition there was a better and more concise built in method so it may be useful for others. Don't think you are alone in approaching your problem in this way. Commented Aug 29, 2014 at 20:06
  • 1
    wrt to whether this should be deleted, you could ask on meta, my feeling is was this post useful for you? do you think others would find it useful? Even if you don't think it was useful the community may disagree or think different where by it may ping pong between vote to close/ reopen/ delete/ undelete. So you could let the community decide. Commented Aug 29, 2014 at 20:10

1 Answer 1

4

There is a built in way:

df.CustomerCard_Num.value_counts()

See the docs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.