Skip to content Skip to sidebar Skip to footer

Csv: How To Find Next Greater Value In A List (python)?

I have a code that reads a column in a CSV file and finds the best match for each element in a column from a given list. There are 3 columns: ZONE, Numbers, and ARPU. We are workin

Solution 1:

You could update your get_plan() function to return the results for all columns at the same time.

Once you have determined the best result, the next highest is the next in common_list. bisect_left() can be used to give you the index of the tuple in the list.

If already at the top of the list you can either return the highest again or None to indicate no higher value. Likewise for the lowest value, it is the next lower in common_list or if the lowest return it or possibly None (as required):

import pandas as pd
from bisect import bisect_left

defget_plans(p):
    best = min(common_list, key=lambda x : abs(x[0] - p['ARPU']))
    best_index = bisect_left(common_list, best)        # get location of best in common_listif best_index < len(common_list) - 1:
        next_greater = common_list[best_index + 1]
    else:
        next_greater = best                     # already highestif best_index > 0:
        next_lower = common_list[best_index - 1]
    else:
        next_lower = best                       # already lowestreturn [*best, *next_greater, *next_lower]


common_list = sorted([
    (1500, "usp15"),
    (2300, "usp23"),
    (2700, "usp27"),
    
    (600,  "bsnspls_s"),
    (1300, "bsnspls_steel"),
    (1800, "bsnspls_chrome"),

    (1000, "bsnsrshn10"),
    (1500, "bsnsrshn15"),
    (2000, "bsnsrshn20"),
])

fields = ['Suggest plan', 'Name 1', 'Next greater', 'Name 2', 'Next lower', 'Name 3']

df = pd.read_csv('root of file.csv')
df[fields] = df.apply(get_plans, axis=1, result_type="expand")
df.to_csv('updated.csv')

This would give:

   ZONE  Numbers  ARPU  Suggest plan         Name 1  Next greater      Name 2  Next lower          Name 3
0     0        5   600           600      bsnspls_s          1000  bsnsrshn10         600       bsnspls_s
1     1       10   300           600      bsnspls_s          1000  bsnsrshn10         600       bsnspls_s
2     2       20   400           600      bsnspls_s          1000  bsnsrshn10         600       bsnspls_s
3     2       30  1300          1300  bsnspls_steel          1500  bsnsrshn15        1000      bsnsrshn10
4     3       20  1400          1300  bsnspls_steel          1500  bsnsrshn15        1000      bsnsrshn10
5     3       20  1450          1500     bsnsrshn15          1500       usp15        1300   bsnspls_steel
6     3       20  1500          1500     bsnsrshn15          1500       usp15        1300   bsnspls_steel
7     4       20  2000          2000     bsnsrshn20          2300       usp23        1800  bsnspls_chrome
8     5       20     0           600      bsnspls_s          1000  bsnsrshn10         600       bsnspls_s

Post a Comment for "Csv: How To Find Next Greater Value In A List (python)?"