0
$\begingroup$

I have a dataset with 30k unique users with each customer having multiple transaction. I aggregated the dataset with one record per user and containing aggregated information such as average transaction amount, last visit date etc. I have a lot of categorical variables. I need to find the most frequently occurring category per each customer and store it in the aggregated data set. How do I do that in Python ? Any referral link or hint is appreciated.

Thanks in advance

$\endgroup$
  • $\begingroup$ Hello, and welcome! Can you please provide an example of what your dataset looks like, and what you expect as a result? It seems to me that your question is purely a python / pandas question, in that case you would certainly get a better answer from Stack Overflow. $\endgroup$ – Romain Reboulleau Oct 2 at 6:11
0
$\begingroup$

Assuming your data looks like:

id, category
1, A
1, B
1, B
2, A
2, A
2, B
2, C
3, D
3, E
4, C
4, C
5, A
6, B

Using Pandas groupby you can achieve your requirement. (df pandas dataframe containing data)

aggregations = {
    'category': lambda x: x.value_counts().index[0]
}
df.groupby('id').agg(aggregations)
$\endgroup$
0
$\begingroup$

I recommend the following instead of value_counts() + accesing the first element. The result is the same, only here you'd be using a function dedicated to what you're asking.

df['most_frequent_category'] = df.groupby('id')['category'].apply(lambda x: x.mode())
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.