Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading