I guess you need to do 2 things.
First you need to convert each row of your dataframe df to corpus files. The following function should do it for you
def CreateCorpusFromDataFrame(corpusfolder,df):
for index, r in df.iterrows():
id=r['ID']
title=r['TITLE']
body=r['BODY']
category=r['CATEGORY']
fname=str(category)+'_'+str(id)+'.txt'
corpusfile=open(corpusfolder+'/'+fname,'a')
corpusfile.write(str(body) +" " +str(title))
corpusfile.close()
CreateCorpusFromDataFrame('yourcorpusfolder/',df)
Second, you need to read the files from yourcorpusfolder and then do the NLTK processing required by you
from nltk.corpus.reader import CategorizedPlaintextCorpusReader
my_corpus=CategorizedPlaintextCorpusReader('yourcorpusfolder/',
r'.*', cat_pattern=r'(.*)_.*')
my_corpus.fileids() # <- I expect values from column ID
my_corpus.categories() # <- I expect values from column CATEGORY
my_corpus.words(categories='cat_A') # <- I expect values from column TITLE and BODY
my_corpus.sents(categories=['cat_A', 'cat_B']) # <- I expect values from column TITLE and BODY
Some helpful references :
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…