Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用下一行值作为条件_Python 3.x_Pandas_Dataframe - Fatal编程技术网

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
。谢谢