In python, iteration over specific condition with dataframe row not working

22 views Asked by At

Help me in correcting the logic to attain the expected output.

import pandas as pd

# Sample DataFrame
data = {
    'phase': ['aaa', 'bbb', 'ccc', 'ddd', 'eee'],
    'phase_input': ['NULL', 'io.flow,io.word,io.des', 'user', '#io.flow,#io.word,io.des', '#io.case,io.word']
}
df = pd.DataFrame(data)


num_inputs_map = {
    'io.flow': ['pf_1', 'pf_2', 'pf_3'],
    'io.word': ['sp_1', 'sp_2'],
    'io.des': ['dc_1'],
    'io case': ['tc_1', 'tc_2', 'tc_3', 'tc_4']  
}

def process_arguments(*args):
    print(f"Processing arguments: {args}")

# Iterate over each row
for index, row in df.iterrows():
    if row['phase_input'].startswith('#'):
        # Argument has multiple values
        arguments = row['phase_input'].split(',')
        processed_args = []

        # Process each argument
        for arg in arguments:
            arg = arg.strip()
            if arg.startswith('#'):
                arg_name = arg[1:]
                inputs = num_inputs_map.get(arg_name, [])
                processed_args.append(inputs)
            else:
                processed_args.append([arg])

        # Generate combinations
        combinations = [combo for combo in zip(*processed_args)]
        for combination in combinations:
            process_arguments(*combination)
    else:
        # Argument has single value
        process_arguments(row['phase_input'])

    

Output acheived through above logic - which is wrong.

Processing arguments: ('NULL',) Processing arguments: ('io.flow,io.word,io.des',) Processing arguments: ('user',) Processing arguments: ('pf_1', 'sp_1', 'io.des')

Expected Output:

Processing arguments: ('NULL',)
Processing arguments: ('io.flow','io.word','io.des')
Processing arguments: ('user',)
Processing arguments: ('pf_1', 'sp_1', 'dc_1')
Processing arguments: ('pf_1', 'sp_2', 'dc_1')
Processing arguments: ('pf_2', 'sp_1', 'dc_1')
Processing arguments: ('pf_2', 'sp_2', 'dc_1')
Processing arguments: ('pf_3', 'sp_1', 'dc_1')
Processing arguments: ('pf_3', 'sp_2', 'dc_1')
Processing arguments: ('tc_1', 'io.word')
Processing arguments: ('tc_2', 'io.word')
Processing arguments: ('tc_3', 'io.word')
Processing arguments: ('tc_4', 'io.word')

Thanks in Advance!

0

There are 0 answers