我无法在下面的函数中创建新的数据框df111。
def term_frequency_new(term, tokenized_document):
codelistWord=[]
codelistCount=[]
for item in term:
codelistWord.append(item)
codelistCount.append(tokenized_document.count(item))
print(codelistWord)
print(codelistCount)
df111=pd.DataFrame({'Words':codelistWord,'Freq':codelistCount})
答案 0 :(得分:1)
尝试在没有全局变量的情况下访问
from collections import Counter
def create_df(lis):
df111 =pd.DataFrame(Counter(lis), index=[0])
create_df(['a','a','b'])
df111.head()#给出错误,因为它不在范围内
具有全局性
from collections import Counter
global df111
def create_df(lis):
global df111
df111 =pd.DataFrame(Counter(lis), index=[0])
create_df(['a','a','b'])
现在您可以访问df111变量
df111.head() #Should print the data
a b
0 2 1