1
\$\begingroup\$

I'm wondering if there is a faster way to do the following code, which works fine. It needs to be applied to every row of the DataFrame df. class_dct contains a different NLTK NaiveBayes classifier for each of 9 regions.

for region in regions:
    new_app=[]
    for title in df.booktitle:
        nlp_genre=class_dct['Classif_%s'% region].classify(features(title))
        new_app.append((title,region,nlp_genre))
\$\endgroup\$

closed as unclear what you're asking by alecxe, t3chb0t, Graipher, Toby Speight, Peilonrayz Jun 12 '17 at 9:12

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1
\$\begingroup\$

I guess the Python interpreter will optimise most or all of this on its own, but you could try a list comprehension and removing an intermediate variable:

new_app = [(title, region, class_dct['Classif_%s'% region].classify(features(title))) for title in df.booktitle]
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.