mardi 21 août 2018

Python Pandas if/else Statement

I am trying to write a nested if/else statement using pandas, but not very great with if statements in pandas. Please see my csv I'm using and what code I've written so far.

df:

t1  
8
1134
0
119
122
446
21
0
138 
0

Current if/else statement logic:

import pandas as pd

df = pd.read_csv('file.csv', sep=';')

def get_cost(df):
    t_zone = 720
    max_rate = 5.5
    rate = 0.0208
    duration = df['t1']

    if duration < t_zone:
        if(duration * rate) >= max_rate:
            return max_rate
        else:
            return(duration * rate)
    else:
        if duration >= 720:
            x = int(duration/720)
            y = ((duration%720) * rate)
            if y >= max_rate:
                return((x * max_rate) + max_rate)
            else:
                return((x * max_rate) + y)

cost = get_cost(df)

This as you probably know raises a ValueError of a Series is ambiguous. If anyone has an ideas or could help translate this if/else statement a more pandas way that would be amazing!

Aucun commentaire:

Enregistrer un commentaire