How to simplify python code that has many if-else statements?

Viewed 69

so I have this code but I don't want to use many if-else condition and I wonder if I could simplify this. Any idea for this? Can I use a loop for this?

    if dh1['date1'][0].strftime("%A") == 'Monday':
        df=df=pd.concat([dh1,dh2.tail(84)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Tuesday':
        df=df=pd.concat([dh1,dh2.tail(96)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Wednesday':
        df=df=pd.concat([dh1,dh2.tail(108)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Thursday':
        df=df=pd.concat([dh1,dh2.tail(120)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Friday':
        df=df=pd.concat([dh1,dh2.tail(132)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Saturday':
        df=df=pd.concat([dh1,dh2.tail(144)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
    elif dh1['date1'][0].strftime("%A") == 'Sunday':
        df=df=pd.concat([dh1,dh2.tail(156)])
        df=df.sort_values(['date1','hr1'])
        df=df.reset_index()
        df=df.drop('index', 1)
5 Answers

It seems that the only difference between all the branches is the argument passed to the tail method. Moreover, the difference between the argument value for the adjacent days is 12, so it can be evaluated as 84 + 12 * weekday counting from Monday as 0. If that's really the case, you can reduce the code like this:

arg = 84 + dh1['date'][0].weekday() * 12
df=df=pd.concat([dh1,dh2.tail(arg)])
df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

The only difference is of df.tail parameter, which depends on date1 column. whenever these cases come you create a mapping like following.

tail_day_map = {
    'Monday': 84,
    'Tuesday': 96,
    'Wednesday': 108,
    'Thursday': 120,
    'Friday': 132,
    'Saturday': 144,
    'Sunday': 156
}

def perform_action(df, tail_day_map):
    tail_number = tail_day_map[df['date1'][0].strftime("%A")]
    df = df.tail(tail_number)
    df = df.sort_values(['date1','hr1'])
    df = df.reset_index()
    df = df.drop('index', 1)
    return df
dict = {
  "Monday": 84,
  "Tuesday": 96,
  ...
}

You can make use of dictionary here

first,
df=df=pd.concat([dh1,dh2.tail(156)]) maybe can be
df=pd.concat([dh1,dh2.tail(156)])
then,

df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

can put out of the if condition.
then for me,
I'll make a dict like: d = {"Monday":84,"Tuesday":96...} on the out of if condition use:

weekday = dh1['date1'][0].strftime("%A")
df=pd.concat([dh1,dh2.tail(d[weekday])])

so the final code like:

d = {"Monday":84,"Tuesday":96...}
weekday = dh1['date1'][0].strftime("%A")
df=pd.concat([dh1,dh2.tail(d[weekday])])
df=df.sort_values(['date1','hr1'])
df=df.reset_index()
df=df.drop('index', 1)

Related