-1

I have the following dataframe and I need to do the following:

-subtract the free package limit from the total number of calls

-multiply the result by the calling plan value

-add the monthly charge depending on the calling plan

There are 2 plan types:

Surf: Monthly charge: $20, 500 monthly minutes

-After exceeding the limits:1 minute: 3 cents

Ultimate: Monthly charge: $70, 3000 monthly minutes

-After exceeding the limits: 1 minute: 1 cent

Here is the function I tried to make and tried to apply it to a new column: https://pastebin.com/iB3GwrQ9

def extra_cost(row):
    calls = row['min_1', 'min_2', 'min_3', 'min_4', 'min_5', 'min_6', 'min_7', 'min_8', 'min_9', 'min_10', 'min_11', 'min_12']
    if plan_name == 'surf':
        if calls > 500:
            return (calls - 500)
        else:
            return "NaN"
    if plane_name == 'ultimate':
        if calls > 3000:
            return (calls - 3000)
        else:
            return "NaN"
    else:
        return "NaN"
user_calls1['sum'] = user_calls1.apply(extra_cost(row))

Here is the dataframe head: https://pastebin.com/8HJWbgUr

user_id first_name last_name plan_name   min_1  min_2  min_3  min_4   min_5  \                                    
1000    Anamaria   Bauer     ultimate     NaN    NaN    NaN    NaN     NaN   
1001    Mickey     Wilkerson surf         NaN    NaN    NaN    NaN     NaN   
1002    Carlee     Hoffman   surf         NaN    NaN    NaN    NaN     NaN   
1003    Reynaldo   Jenkins   surf         NaN    NaN    NaN    NaN     NaN   
1004    Leonila    Thompson  surf         NaN    NaN    NaN    NaN  181.58   
                                     
user_id first_name last_name plan_name   min_6   min_7   min_8   min_9  \                                
1000    Anamaria   Bauer     ultimate      NaN     NaN     NaN     NaN   
1001    Mickey     Wilkerson surf          NaN     NaN  171.14  297.69   
1002    Carlee     Hoffman   surf          NaN     NaN     NaN     NaN   
1003    Reynaldo   Jenkins   surf          NaN     NaN     NaN     NaN   
1004    Leonila    Thompson  surf       261.32  358.45  334.86  284.60   

                                                                        
user_id first_name last_name plan_name    min_10  min_11   min_12                       
1000    Anamaria   Bauer     ultimate      NaN     NaN   116.83  
1001    Mickey     Wilkerson surf       374.11  404.59   392.93  
1002    Carlee     Hoffman   surf        54.13  359.76   363.24  
1003    Reynaldo   Jenkins   surf          NaN     NaN  1041.00  
1004    Leonila    Thompson  surf       341.63  452.98   403.53

I have not been able to figure out why it is not working and returning " name 'row' is not defined ". If there is a better solution, please let me know!

Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
Chris
  • 1
  • 1
  • Does `calls` for the user refer to the sum of all the `min_x` columns of their row? – Balaji Ambresh Jul 05 '20 at 19:56
  • A function is the first positional argument of apply. Also, specify axis = 1 to apply function to each row. Thus try: `user_calls1['sum'] = user_calls1.apply(extra_cost, axis = 1)` – DarrylG Jul 05 '20 at 20:00

1 Answers1

0

Could you give this a shot?

import numpy as np
def extra_cost(row):
    calls = row[['min_1', 'min_2', 'min_3', 'min_4', 'min_5', 'min_6', 'min_7', 'min_8', 'min_9', 'min_10', 'min_11', 'min_12']]
    if row.plan_name == 'surf':
        total = calls[calls > 500].sum()
        if total != 0:
            return total
        else:
            return np.nan
    if row.plan_name == 'ultimate':
        total = calls[calls > 3000].sum()
        if total != 0:
            return total
        else:
            return np.nan
    else:
        return np.nan

user_calls1['sum'] = user_calls1.apply(extra_cost, axis=1)
Balaji Ambresh
  • 4,977
  • 2
  • 5
  • 17
  • I tried and it didn't work. I got this message 'Series' object has no attribute 'plan_name'", 'occurred at index (1000, Anamaria, Bauer, ultimate)' – Chris Jul 05 '20 at 20:21
  • @Chris sorry about the typo. I typed it as `plane_name`. Could you try it now? – Balaji Ambresh Jul 05 '20 at 20:26
  • Yeah I did fix it when I pasted the code and still had the same issue – Chris Jul 05 '20 at 20:26
  • Does `user_calls1` have a column named `plan_name` ? Try `print(user_calls1.columns)` – Balaji Ambresh Jul 05 '20 at 20:30
  • It doesn't show up under the info. For some reason user_id, first_name, last_name and plan_name don't show up. It came from merging a pivot_table function, so idk if that is why. Oh I used it as an index – Chris Jul 05 '20 at 20:40
  • I will reset_index and see if that works – Chris Jul 05 '20 at 20:45
  • So I got it to work. It is almost there, but I am not getting the right values. It is adding all the numbers from the columns and subtracting 500. I need it to subtract 500 from each column that is above 500 and add all those values together. So for instance if the values were 510, 400, 200 and 550, the value for the new column would be 60 – Chris Jul 05 '20 at 21:40
  • @Chris updated post. check it out. – Balaji Ambresh Jul 06 '20 at 04:05