1

I have a code with for loop like below and it ends with some if statements;

def distribution_selection(csv_file=None,product_column=None,demand=None):
    out = [] 

    for num in df_all[product_column]:
            .
            .
            .

    if best_fit=="norm":
        print("Product:",num)
        print("Best fit:",best_fit)
        print("Likelihood:", likelihoods[best_fit])
        print("Parameters:", mean,std)

    if best_fit=="nbinom": 
        print("Product:",num)
        print("Best fit:",best_fit)
        print("Likelihood:", likelihoods[best_fit])
        print("Parameters:", p_nbinom,r_binom)

    if best_fit=="poisson": 
        print("Product:",num)
        print("Best fit:",best_fit)
        print("Likelihood:", likelihoods[best_fit])
        print("Parameters:", lambda_)

And let's say here's the results;

Product: 001.001
Best fit: nbinom
Likelihood: 6.317496035718443e-15
Parameters: 0.002660521439486909 0.41659311972644725
Product: 001.002
Best fit: nbinom
Likelihood: 5.902081129467898e-18
Parameters: 0.005335820123825622 0.7249662663271113
Product: 001.003
Best fit: nbinom
Likelihood: 2.871871246304317e-13
Parameters: 0.00743701201046538 0.45081292375812926
Product: 001.004
Best fit: poisson
Likelihood: 0.0002870492567273848
Parameters: 15.333333333333334

How can I make print output dataframe as follows? Could you please help me about this?

Products     BestFit      Likelihood              ParameterA             ParameterB
001.001      nbinom       6.317496035718443e-15   0.002660521439486909   0.41659311972644725
001.002      nbinom       5.902081129467898e-18   0.005335820123825622   0.7249662663271113
001.003      nbinom       2.871871246304317e-13   0.00743701201046538    0.45081292375812926
001.004      poisson      0.0002870492567273848   15.333333333333334     NA
1

1 Answer 1

1

Create list of dictionaries in loop with append and then pass to DataFrame constructor:

df_all = pd.DataFrame({'a':[2,3]})

def distribution_selection(csv_file=None,product_column=None,demand=None):
    out = []

    #sample data
    best_fit = 'norm'
    num, mean, std = 1, .03, .05
    likelihoods = {'norm':2}

    for num in df_all[product_column]:

        if best_fit=="norm":
            print("Product:",num)
            print("Best fit:",best_fit)
            print("Likelihood:", likelihoods[best_fit])
            print("Parameters:", mean,std)
            d = {'Product':num,
                 "Best fit:":best_fit,
                 "Likelihood":likelihoods[best_fit],
                 "Parameters1":mean,
                 "Parameters2":std}
            out.append(d)

            ...
            #similar for each conditions

    return pd.DataFrame(out)

df = distribution_selection('file', 'a')
print (df)
   Product Best fit:  Likelihood  Parameters1  Parameters2
0        2      norm           2         0.03         0.05
1        3      norm           2         0.03         0.05
Sign up to request clarification or add additional context in comments.

4 Comments

hi @jazrael, i' ve edited my question, that for loop is in a function and dataframe is empty when i do what you say.
@Salih - There is also out.append(d) ?
yes,and i got this; NameError: name 'out' is not defined
@Salih - answer was edited, for me working nice (sample data only, so same values in both rows in output DataFrame)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.