简体   繁体   中英

Why does Pandas convert my DataFrame into a DataFrame and a Series inside a for loop?

arms_bayes_list = pandas.DataFrame(
            {'arms': pandas.Series(arms_bayes), 'priori_mean': pandas.Series(arm_priori_mean_vectorized(arms_bayes)),
             'variance_square': pandas.Series(arm_variance_square_vectorized(arms_bayes)),
             'posterior_mean': pandas.Series(arm_posterior_mean_vectorized(arms_bayes)),
             'posterior_variance_square': pandas.Series(arm_posterior_variance_square_vectorized(arms_bayes)),
             'empirical_mean': pandas.Series(arm_empirical_mean_vectorized(arms_bayes)),
             'mean': pandas.Series(arm_mean_vectorized(arms_bayes)),
             'priori_variance_square': pandas.Series(arm_variance_square_vectorized(arms_bayes))})

        optimal_mean_bayes = numpy.amax(arms_bayes_list[["mean"]])
        print(type(arms_bayes_list))
         # This gives a datatype <class'pandas.core.frame.DataFrame'>
        for round_no in range(int(no_of_rounds)):
            print(type(arms_bayes_list))
            # This gives a datatype <class'pandas.core.frame.DataFrame'><class      'pandas.core.series.Series'>

Iterating over the data frame I get the error that Series has no attribute called 'itertuples'/ 'iterrow' so I really can't iterate over the DataFrame.

Well, this is rather mysterious to me. For some reason the type of the DataFrame is different inside the for loop than what it is outside the for loop. I don't think I've made any mistakes but I another set of eyes would definitely help.

You can try printing arms_bayes_list.__class__ instead of arms_bayes_list

replace your line of print(type(arms_bayes_list)) to this print(arms_bayes_list. class ) Took the liberty of doing it for you.

arms_bayes_list = pandas.DataFrame(
    {'arms': pandas.Series(arms_bayes), 
     'priori_mean': pandas.Series(arm_priori_mean_vectorized(arms_bayes)),
     'variance_square': pandas.Series(arm_variance_square_vectorized(arms_bayes)),
     'posterior_mean': pandas.Series(arm_posterior_mean_vectorized(arms_bayes)),
     'posterior_variance_square': pandas.Series(arm_posterior_variance_square_vectorized(arms_bayes)),
     'empirical_mean': pandas.Series(arm_empirical_mean_vectorized(arms_bayes)),
     'mean': pandas.Series(arm_mean_vectorized(arms_bayes)),
     'priori_variance_square': pandas.Series(arm_variance_square_vectorized(arms_bayes))
    })

optimal_mean_bayes = numpy.amax(arms_bayes_list[["mean"]])

for round_no in range(int(no_of_rounds)):
    print(arms_bayes_list.__class__)
    # ... rest of the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM