Python 3.x 使用下一行值作为条件
假设我解析了一个日志文件并将其放入Python 3.x 使用下一行值作为条件,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,假设我解析了一个日志文件并将其放入pandas.DataFrame中 我想创建一个新的boolean列,只有当当前行中有EXPRESSION\u 1字符串,并且下一行中有EXPRESSION\u 2表达式时,该列才会有True 我可以只对一个表达式执行此操作,如下面的示例1所示: 示例1: import pandas as pd EXPRESSION_1 = 'Starts streaming the stream rtspsrc' EXPRESSION_2 = 'initializing
pandas.DataFrame
中
我想创建一个新的boolean
列,只有当当前行中有EXPRESSION\u 1
字符串,并且下一行中有EXPRESSION\u 2
表达式时,该列才会有True
我可以只对一个表达式执行此操作,如下面的示例1
所示:
示例1:
import pandas as pd
EXPRESSION_1 = 'Starts streaming the stream rtspsrc'
EXPRESSION_2 = 'initializing gst pipeline'
df = pd.DataFrame(
{
'message': [
'Some log text',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
]
}
)
df.loc[:, 'process_started'] = df.loc[:, 'message'].apply(lambda msg: True if msg.find(EXPRESSION_1) > -1 else False)
df
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc True
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False # <= Note the False here
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
示例1的输出:
import pandas as pd
EXPRESSION_1 = 'Starts streaming the stream rtspsrc'
EXPRESSION_2 = 'initializing gst pipeline'
df = pd.DataFrame(
{
'message': [
'Some log text',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
]
}
)
df.loc[:, 'process_started'] = df.loc[:, 'message'].apply(lambda msg: True if msg.find(EXPRESSION_1) > -1 else False)
df
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc True
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False # <= Note the False here
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
所需输出:
import pandas as pd
EXPRESSION_1 = 'Starts streaming the stream rtspsrc'
EXPRESSION_2 = 'initializing gst pipeline'
df = pd.DataFrame(
{
'message': [
'Some log text',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
]
}
)
df.loc[:, 'process_started'] = df.loc[:, 'message'].apply(lambda msg: True if msg.find(EXPRESSION_1) > -1 else False)
df
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc True
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False # <= Note the False here
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
消息处理\u已启动
0某些日志文本为False
1一些日志文本错误
2开始流式传输流rtspsrc False 35;您可以使用shift
操作来执行此操作。代码中的shift(-1)
将消息
列向上移动1(简而言之):
输出:
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
在答案中找到答案:
输出:
import pandas as pd
EXPRESSION_1 = 'Starts streaming the stream rtspsrc'
EXPRESSION_2 = 'initializing gst pipeline'
df = pd.DataFrame(
{
'message': [
'Some log text',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
'Starts streaming the stream rtspsrc',
'initializing gst pipeline',
'Some log text',
]
}
)
df.loc[:, 'process_started'] = df.loc[:, 'message'].apply(lambda msg: True if msg.find(EXPRESSION_1) > -1 else False)
df
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc True
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False # <= Note the False here
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
message process_started
0 Some log text False
1 Some log text False
2 Starts streaming the stream rtspsrc False
3 Some log text False
4 Some log text False
5 Starts streaming the stream rtspsrc True
6 initializing gst pipeline False
7 Some log text False
好主意,尽管它不适合我的设置,因为我的数据框中的消息
列将不仅仅是EXPRESSION\u 1
或EXPRESSION\u 2
。谢谢