Python Forum
Use of & operator, groupby in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use of & operator, groupby in python
#1
ID AMOUNT DATE AVG_AMOUNT
FACL01 10 20201503
FACL02 20 20201503
FACL03 30 20201503
FACL01 40 20201502
FACL01 50 20201503
FACL06 60 20201502
FACL07 70 20201501
FACL08 80 20201503
FACL09 20 20201503
FACL01 10 20201501
FACL11 30 20201503
FACL12 10 20201502


Assumptions: Today is March 15 2020 (20201503). This data is for last 3 months.


    import pandas as pd
    df= pd.DataFrame({ 'ID': [FACL01, FACL02, FACL03, FACL01, FACL04, FACL06, FACL07, FACL08, FACL09, FACL01, FACL11, FACL12], 'AMOUNT': [10, 20, 30, 40, 50, 60, 70, 80, 20, 10, 30, 10], 'DATE': [20201503, 20201503, 20201503, 20201502, 20201503, 20201502, 20201501, 20201503, 20201503, 20201501, 20201503, 20201502]})
    M1=[20201501, 20201502, 20201503] 
    #My try: 
    df["AVG_AMOUNT"] = df[df["DATE"].isin(M1)].groupby('ID')['AMOUNT'].mean()
So every month this list gets updated with an ID number and corresponding amount etc. For eg, FACL01 came in Jan, Feb and March.

Goal is to fill current month's ID (say,FACL01 for 20201503) with mean of amount for last 3 months for that ID ie. ((10+40+80)/3). (PS:I know the last part makes it a bit complex)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to iterate Groupby in Python/PySpark DrData82 2 2,872 Feb-05-2022, 09:59 PM
Last Post: DrData82
  python matplotlib groupby okpython 0 1,149 Feb-08-2021, 11:09 AM
Last Post: okpython
  Python greater than equal to comparison operator Ilangos 4 2,453 Sep-26-2020, 03:53 AM
Last Post: buran
  Python logical operator AND rasec70 4 2,545 May-07-2020, 03:40 PM
Last Post: pyzyx3qwerty
  Comparison Operator "is" idle vs python command spisatus 3 2,813 Oct-29-2019, 10:00 PM
Last Post: DeaD_EyE
  Is there any <> operator in python 3.6 rajeev1729 1 3,138 Sep-11-2017, 07:46 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020