I am trying to write a nested if/else statement using pandas, but not very great with if statements in pandas. Please find the sample CSV data being processed and the sample code snippet 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 snippet raises a ValueError: The truth value of a Series is ambiguous error. If anyone has better solutions or could help translate this if/else statement a more pandas way that would be amazing!
print(duration)and I'm sure you will figure this out