If input month is missing get the next nearest smallest or equal too number from DF

Viewed 26

I need a help in python where if a sequence of month is missing as per user input then it will give the output rows as either equal too or nearest smallest numbers from Months in all VIN.

CurrentDf:

VIN Total Age Months Monthly Revenue
v1 10 00 1513
v1 10 05 1108
v1 10 07 4330
v1 10 09 7121
v2 08 01 1998
v2 08 04 4997
v2 08 05 8528
v2 08 06 1783
v2 08 07 9628
v2 08 08 2082

Age = [int(x) for x in input('Please enter Months?= ').split(',')]

If UserInput: 6

outputDF:

VIN Total Age Months Monthly Revenue
v1 10 05 1108
v2 08 06 1783
1 Answers

Try:

age = 6

x = df[df["Months"] <= age].groupby("VIN", as_index=False).tail(1)
print(x)

Prints:

  VIN  Total Age  Months  Monthly Revenue
1  v1         10       5             1108
7  v2          8       6             1783
Related