Home > Mobile >  Pandas - groupby by continues range
Pandas - groupby by continues range

Time:03-17

I have a dataframe of transactions:

user_id mcc_code    currency_rk transaction_amt transaction_dttm    transaction_date

I want to know mean of count transactions by user_id by day, how can I achieve this? I think I need mark continues ranges of dates and groupby on it (because df sorted by user_id).

CodePudding user response:

Let's assume your dataframe is named df, writing

new_df = df.group_by(["user_id", "day"]).loc[:, whatever_transaction_is].transform("count")

will give you the count of transaction by user by day. Taking mean of new_df will lead to your desired output.

CodePudding user response:

Answer:

transactions.groupby(["user_id", "transaction_date"])['currency_rk'].count().groupby(['user_id']).mean()
  • Related