'locating values in rows and get other rows related to the found row
I have a data frame contains texts, timecode, and date and time.
I have a function to find a key word in any row, then see if there is another hit of the keyword in the next two minutes, if there are two rows contains same keyword within 2 minutes I would take the rows in between depending on the timecode AKA timestamp, if there is only one row with the key word in two minutes, I would take text 30 seconds before and after the row that contains the keyword I am looking for.
here is snip of the data frame
the result is a data frame where each row represent multiple rows from the original data frame.
the result data frame should have:
channel ID
date of the recoding
combined timestamps for all rows we combined: For example, if there is only one row with key word, we will combine previous 30 second and next 30 sec of the record, which are located in other rows. the timestamp in the result will begin from the earliest row we combined until the last one.
here is the function I have.
# get keyword hit and get needed rows to make conversation
# df is dataframe of one channel
def get_conversation(df):
# searching for each keyword
for key in range(len(keywords)):
# locating hit
for row, index in df.iterrows():
if row['Cat_Frames'].str.contains(key['Keyword'] ,False, regex=True) == keywords['keyword']:
print(keywords[key] + ' is in : '+row['Cat_Frames'])
#Check if same record
NextRow = row +1
if row['Created_At'] == NextRow['Created_At'] & row['Finished_At'] == NextRow['Created_At']:
print('next row is continuing the original : ' + NextRow['Cat_frames'] +' Timecode: '+ NextRow['Timecode'])
# get 30 Sec0
#row['Timecode']
return ' '
the function should append multiple rows we combined as one row in the result data frame
It is not complete it has errors. I am stuck and have no idea of how to approach this.
I can use help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
