Home > Mobile >  how to convert function output into list, dict or as data frame?
how to convert function output into list, dict or as data frame?

Time:12-09

My issue is, i don't know how to use the output of a function properly. The output contains multiple lines (j = column , i = testresult) I want to use the output for some other rules in other functions. (eg. if (i) testresult > 5 then something)

I have a function with two loops. The function goes threw every column and test something. This works fine.

def test():    

    scope = range(10)
    scope2 = range(len(df1.columns))
    for (j) in scope2:
          for (i) in scope:
              if df1.iloc[:,[j]].shift(i).loc[selected_week].item() > df1.iloc[:,[j]].shift(i 1).loc[selected_week].item():
                  i   1
              else:
                print(j,i)
                
                break

Output: test()

1 0
2 3
3 3
4 1
5 0
6 6
7 0
8 1
9 0
10 1
11 1
12 0
13 0
14 0
15 0

I tried to convert it to list, dataframe etc. However, i miss something here.

What is the best way for that?

Thank you!

CodePudding user response:

A fix of your code would be:

def test():    
    out = []
    scope = range(10)
    scope2 = range(len(df1.columns))
    for j in scope2:
        for i in scope:
            if df1.iloc[:,[j]].shift(i).loc[selected_week].item() <= df1.iloc[:,[j]].shift(i 1).loc[selected_week].item():
                 out.append([i, j])
    return pd.DataFrame(out)

out = test()

But you probably don't want to use loops as it's slow, please clarify what is your input with a minimal reproducible example and what you are trying to achieve (expected output and logic), we can probably make it a vectorized solution.

  • Related