Issue
I have code that looks like this
df = pd.read_csv('data.csv', nrows=100000)
def get_time(event_1, event_2):
clean = df[(df['event_name'] == event_1) | (df['event_name'] == event_2)]
...
final_df = get_time(event_1 = 'open', event_2 = 'close')
So what I want to do is to have my original data without filtering if event_1 = 'open', event_2 = 'close'. In the other cases I want it to filter like in line with clean variable Original output
date event
2022/10/05 open
2022/10/06 jump
2022/10/05 run
2022/10/06 close
Expected when event_1 = 'open', event_2 = 'close'
date event
2022/10/05 open
2022/10/06 jump
2022/10/05 run
2022/10/06 close
Expected when event_1 = 'open', event_2 = 'jump'
date event
2022/10/05 open
2022/10/06 jump
Appreciate your help
Solution
Use a simple if
statement
def get_time(event_1, event_2):
if event_1 == 'open' and event_2 == 'close':
clean = df
else:
clean = df[(df['event_name'] == event_1) | (df['event_name'] == event_2)]
...
Answered By - Barmar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.