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)
-
optimize your code and use match caseMehmaam– Mehmaam2022年08月18日 06:01:49 +00:00Commented Aug 18, 2022 at 6:01
5 Answers 5
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)
Comments
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
3 Comments
84 + 12 * weekday_int%A returns a locale-specific day name, so it will break if it runs on any computer with non-English language.If you're able to use Python 3.10 or later, how about using Match Case? https://learnpython.com/blog/python-match-case-statement/
Match Case may simplify your code and provide more readability, but as mentioned in the second link be cautious with the order of the cases as it may change the behavior of the logic.
Otherwise, there may be other flow control strategies you can use: https://docs.python.org/3/tutorial/controlflow.html
Comments
dict = {
"Monday": 84,
"Tuesday": 96,
...
}
You can make use of dictionary here
Comments
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)