[Python] Pandas: Use the (find by 'left, mid and right' columns) method to extract data from...

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to extract a [True, False, True] pattern, in a pandas index_row. Here's my sample csv.file for reproduction:

    import pandas as pd
    import numpy as np


    Here's a CSV file:

    df = pd.read_csv('My_Boxes.csv')
    pd.set_option('display.max_columns', None)
    df = df.drop(columns=['Unnamed: 0'])
    df = df.loc[7: 9]
    df


    This is how the data frame looks like on output:

    13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
    7 0 0 0 0 0 0 0 0 0 0 0 0 0 26 27 0
    8 0 0 15 0 17 0 0 0 0 0 0 0 0 26 0 28
    9 0 0 15 0 0 0 0 0 0 0 23 24 0 0 0 0


    I'm trying to find the presence of a formation where, two opposite columns both carry a non zero integer and are, separated by a middle column whose value is '0'.

    My dataframe does carry two instances of such a formation in row[8], i.e. [15, 0, 17] and [26, 0, 28].

    I want to create a function that shall iterate through a dataframe, confirm such a presence and return result(s) as [True, False, True].

    Here's what I've done, so far, in trying to construct my thoughts:

    row8 = df.loc[8]
    row = [(row8.iloc != 0), (row8.iloc==0), (row8.iloc !=0)]
    row



    This how the output looks like:

    `True, False, True`


    My challenge is to align this output to the core function:

    I've done a number of back tests and have, concluded that my constructor is responding, to the prescribed function rather than perform, an iter_df function.

    My expectation was that the function would, return a true copy of the data frame representative, maybe an axis=1, inplace=True return.

    My thoughts are are aligned more towards something of this outcome:

    row = row[['index_name'], left_col['name'], mid_col['name'],
    right_col['name']]

    result1 = index_name = list(15, 0, 17)
    result2 = index_name = list(26, 0, 28)


    A following (or, an even better) function is, what I'm trying to achieve:

    output1 = result1, i.e. (axis=0, inplace=True)
    output2 = result2, i.e. (axis=0, inplace=True)


    I would appreciate all the help necessary, thanking you in advance. An accurate result should, be focused on such a following; return:

    match1 = ['15', '0', '17']
    match2 = ['26', '0', '28']


    Thanks, once more, to you all for your patience.

    Continue reading...

Compartilhe esta Página