Python 熊猫-名称错误:名称';df2&x27;没有定义

Python 熊猫-名称错误:名称';df2&x27;没有定义,python,pandas,dataframe,Python,Pandas,Dataframe,对于当前项目,我正在运行Pandas数据帧的多次迭代,并计划打印变量df2 当调用行print(df2)时,我得到了错误namererror:name'df2'未定义。我已经在检查解决方案,但还没有找到任何解决方案。是否有任何聪明的调整,使这个运行 相应的代码部分如下所示: # Open the file to write to with open('sp500-1.csv', 'w', newline='') as file: writer = csv.writer(file)

对于当前项目,我正在运行Pandas数据帧的多次迭代,并计划打印变量
df2

当调用行
print(df2)
时,我得到了错误
namererror:name'df2'未定义
。我已经在检查解决方案,但还没有找到任何解决方案。是否有任何聪明的调整,使这个运行

相应的代码部分如下所示:

# Open the file to write to
with open('sp500-1.csv', 'w', newline='') as file:
    writer = csv.writer(file)

    # Write headers
    writer.writerow(["Section", "TFI"])

    # Loop over the JSON objects
    for i in ['txt_pro','txt_con','txt_adviceMgmt','txt_main']:

        # Loop over the common words inside the JSON object
        common_words = get_top_n_bigram_Group2(df[i], 500)
        for word in common_words:

            # Print and write row.
            print(df2)
            writer.writerow([df2])
定义
df2
的代码如下:

def get_top_n_bigram_Group2(corpus, n=None):
    # settings that you use for count vectorizer will go here
    tfidf_vectorizer=TfidfVectorizer(ngram_range=(2, 2), stop_words='english', use_idf=True).fit(corpus)

    # just send in all your docs here
    tfidf_vectorizer_vectors=tfidf_vectorizer.fit_transform(corpus)

    # get the first vector out (for the first document)
    first_vector_tfidfvectorizer=tfidf_vectorizer_vectors[0]

    # place tf-idf values in a pandas data frame
    df1 = pd.DataFrame(first_vector_tfidfvectorizer.T.todense(), index=tfidf_vectorizer.get_feature_names(), columns=["tfidf"])
    df2 = df1.sort_values(by=["tfidf"],ascending=False)

    return df2

嘿,这只是一个简单的问题。 请参见函数返回变量,但返回到调用它的变量

# Open the file to write to
  with open('sp500-1.csv', 'w', newline='') as file:
writer = csv.writer(file)

# Write headers
writer.writerow(["Section", "TFI"])

# Loop over the JSON objects
for i in ['txt_pro','txt_con','txt_adviceMgmt','txt_main']:

    # Loop over the common words inside the JSON object
    common_words = get_top_n_bigram_Group2(df[i], 500)
    for word in common_words:

        # Print and write row.
        print(common_words)
        writer.writerow([word])

嘿,这只是一个简单的问题。 请参见函数返回变量,但返回到调用它的变量

# Open the file to write to
  with open('sp500-1.csv', 'w', newline='') as file:
writer = csv.writer(file)

# Write headers
writer.writerow(["Section", "TFI"])

# Loop over the JSON objects
for i in ['txt_pro','txt_con','txt_adviceMgmt','txt_main']:

    # Loop over the common words inside the JSON object
    common_words = get_top_n_bigram_Group2(df[i], 500)
    for word in common_words:

        # Print and write row.
        print(common_words)
        writer.writerow([word])

发生这种情况是因为df2是在函数内部定义的,并且它遵循,所以它只存在于函数定义内部

因为您返回了它并将另一个名称传递给它:

common_words = get_top_n_bigram_Group2(df[i], 500)
因此,您返回df2值并将其作为常用单词传递

然后,您将对其进行迭代:

for word in common_words:

因此,在printing和writerow函数中应该使用word,而不是df2。

发生这种情况是因为df2是在函数中定义的,并且它服从,因此它只存在于函数定义中

因为您返回了它并将另一个名称传递给它:

common_words = get_top_n_bigram_Group2(df[i], 500)
因此,您返回df2值并将其作为常用单词传递

然后,您将对其进行迭代:

for word in common_words:

因此,在您的打印和书写功能中应该使用word,而不是df2。

好吧,当您运行
get\u top\u bigram\u Group2
时,您将
df2
存储到
common\u words
好吧,当您运行
get\u top\u n\u bigram\u Group2
时,您将
df2
存储到
common\u words

中,实际上您并没有在全局范围内定义
df2
(即,在函数
get\u top\u n\u bigram\u Group2
之外)。您可以命名
get\u top\u n\u bigram\u Group2
common\u words
,因此应该使用
print(common\u words)
。名称
df2
仅存在于函数中。感谢您的大力支持。如果我理解正确,
common\u words
将在本例中产生
df2
变量,该变量已在
def get\u top\u n\u bigram\u Group2
中定义。另外请注意,数据帧有一个
。to\u csv
方法可能会使您的生活更轻松。谢谢,让我看看如何集成它。目前,代码只在终端中打印了正确的输出,但没有在.csv文件中写入行。您实际上没有在全局范围内定义
df2
(即,在函数
get\u top\n\u bigram\u Group2
之外)。您可以命名
get\u top\u n\u bigram\u Group2
common\u words
,因此应该使用
print(common\u words)
。名称
df2
仅存在于函数中。感谢您的大力支持。如果我理解正确,
common\u words
将在本例中产生
df2
变量,该变量已在
def get\u top\u n\u bigram\u Group2
中定义。另外请注意,数据帧有一个
。to\u csv
方法可能会使您的生活更轻松。谢谢,让我看看如何集成它。目前,代码只在终端中打印了正确的输出,但没有在.csv文件中写入行,其中common_words将为您提供完整的object&word将为您提供一个recordThank,这使代码得以运行。但是,它不会将任何变量写入.csv文件(我收到的输出是4x
tfidf
)。是的,也许您只需要调整函数,使其返回预期结果。非常感谢您接受常见单词将为您提供的内容,然后整个对象和单词将为您提供一条记录。谢谢,这使代码得以运行。但是,它不会将任何变量写入.csv文件(我收到的输出是4x
tfidf
)。是的,也许您只需要调整函数,使其返回预期结果。非常感谢您的接受