-1

I wanted to convert following multiple lines of code to a single line. I have tried but i am unable to achieve final result.

Multiple line of code :-

for k, v in FLOW_GROUPS.items():
    if k == '':
        [pd.Series(df_by_fulfillment_flow[v].sum(axis=1), name=k)]
    else:
        [pd.Series(df_by_fulfillment_flow[v].mean(axis=1), name=k)]

my success so far is given below, i want to add if condition also to it :-

[pd.Series(df_by_fulfillment_flow[v].sum(axis=1), name=k) for k, v in FLOW_GROUPS.items()]
Vibhor Gupta
  • 670
  • 7
  • 16

1 Answers1

1

ternary loop with if else Syntax:

[<ifvalue> if <condition> else <elsevalue> for i in <yourdata>]

ternary loop with if statement only Syntax:

[<value> for i in <yourdata> if <condition>]

Although I dont know your input and desire output, but with syntax your single line code should be like:

[[pd.Series(df_by_fulfillment_flow[v].sum(axis=1), name=k)] if k == '' else [pd.Series(df_by_fulfillment_flow[v].mean(axis=1), name=k)] for k, v in FLOW_GROUPS.items()]
R. Baraiya
  • 1,490
  • 1
  • 4
  • 17
  • 1
    You are close to solution which worked for me. Solution which worked for me is ```[pd.Series(df_by_fulfillment_flow[v].sum(axis=1), name=k) if k == 'operational_result' else pd.Series(df_by_fulfillment_flow[v].mean(axis=1), name=k) for k, v in FLOW_GROUPS.items()] ``` – Vibhor Gupta Oct 12 '22 at 12:39