【发布时间】:2019-10-17 22:13:59
【问题描述】:
我有一个包含几列的数据框。其中一个名为'log_text'. 我想在此列中查找具有匹配字符串的行对。
例如,如果'log_text' 有这些字符串
Device remove ID#xxx
Device remove ID#yyy
Device remove ID#zzz
Device arrive ID#xxx
Device arrive ID#yyy
Device arrive ID#zzz
目标:
我想获取包含'Device remove ID#xxx' 和'Device arrive ID#xxx' 的行并能够对它们的其他列进行处理,然后对包含'Device remove ID#yyy' 和'Device arrive ID#yyy' 等的行重复此操作。
我尝试的是使用iterrows(),找到当前行的ID#,从表中删除该行,然后找到包含匹配ID#字符串的第一行。
for index, row in temp_df.iterrows():
log_string = row['log_text']
id_text = log_string.partition("ID#")[2]
temp_df.drop(row)
match = temp_df[temp_df['log_text'].str.contains(id_text)]
# Somehow stash the 2 rows together somewhere?
# like stash[index,1] = row; stash[index,2] = match;
temp_df.drop(match)
【问题讨论】: