如何使用熊猫数据框作为全局变量?

时间:2019-04-02 11:11:52

标签: python pandas flask

我正在运行flask应用程序,并在merged函数中定义的read_uploaded_file函数中使用变量data_prediction

现在我明白这可能是因为我没有在使用它的函数中全局或局部分配合并变量。有人可以建议我如何在此处使用数据框merged或更好的实现方式吗?

错误:

count_by_prediction = merged.groupby('Prediction').count()[['Incident Number']].sort_values(by=['Incident Number'], ascending=False)
UnboundLocalError: local variable 'merged' referenced before assignment

代码:

def data_prediction(filename):

    model_name = 'SVM.sav'
    SVM = pickle.load(open(model_name, 'rb'))

    df_prediction = pd.read_csv(filename,encoding = "ISO-8859-1")
    df_prediction = df_prediction.applymap(lambda x: x.strip() if isinstance(x, str) else x)

    df_prediction["Description"].fillna(" ", inplace = True) 
    df_prediction['Full description'] = df_prediction['Short Description'] + " " +  df_prediction['Description']
    X_predict = df_prediction['Full description']
    display(X_predict)

    documents_predict = []

    for sen in range(0, len(X_predict)):
        # Remove all the special characters
        document = re.sub(r'\W', ' ', str(X_predict[sen]))

        # remove all single characters
        document = re.sub(r'\s+[a-zA-Z]\s+', ' ', document)

        # Remove single characters from the start
        document = re.sub(r'\^[a-zA-Z]\s+', ' ', document) 

        # Substituting multiple spaces with single space
        document = re.sub(r'\s+', ' ', document, flags=re.I)

        # Removing prefixed 'b'
        document = re.sub(r'^b\s+', '', document)

        # Converting to Lowercase
        document = document.lower()

        documents_predict.append(document)

    data_for_predict = pd.Series(documents_predict)
    predicted_svm_actual_data = SVM.predict(data_for_predict.values.astype('U'))
    output=pd.DataFrame(data={"Description":data_for_predict,"Prediction":predicted_svm_actual_data})
    merged = pd.merge(left=df_prediction, left_index=True,right=output, right_index=True,how='inner')
    columns = ['Description_x', 'Description_y']
    merged.drop(columns, inplace=True, axis=1)
    print('HHH')
    print(merged)



    #Provide the name of output file. it will contain the description and predicted output/category
    writer = pd.ExcelWriter(r"predicted_output.xlsx", engine='xlsxwriter')
    merged.to_excel(writer, sheet_name='Sheet1')
    writer.save()
    print(merged)
    return merged



@app.route('/read_file', methods=['GET'])
def read_uploaded_file():
    filename = secure_filename(request.args.get('filename'))
    product = request.args.get("product")

    try:
        if filename and allowed_file(filename):
            if(product=='itanywhere'):
                print('itanywhere is happening')
                merged = data_prediction(filename)                
    except IOError:
        pass
    send_from_directory(directory=UPLOAD_FOLDER, filename='predicted_output_new_2.xlsx')

    count_by_prediction = merged.groupby('Prediction').count()[['Incident Number']].sort_values(by=['Incident Number'],\
                                                                                                ascending=False)
    display(merged)
    plt.figure(figsize = (20,8))
    plt.xticks(rotation=90)
    #plt.tight_layout()
    sn.countplot('Prediction', data=merged)
    img = io.BytesIO()  # create the buffer
    plt.savefig(img, format='png',bbox_inches = "tight")  # save figure to the buffer
    img.seek(0)  # rewind your buffer
    plot_data = urllib.parse.quote(base64.b64encode(img.read()).decode()) # base64 encode & URL-escape
    return render_template('data.html',plot_url=plot_data,tables_summary=[count_by_prediction.to_html(classes='data')], titles_summary=count_by_prediction.columns.values,\
                           tables=[merged.to_html(classes='data')], titles=merged.columns.values)




if __name__ == "__main__":
    app.run(host='0.0.0.0')

1 个答案:

答案 0 :(得分:1)

在任何函数外部声明合并为全局变量,然后在函数内部使用global关键字使用它。当您对data_prediction中的合并进行任何更改时,它也会在read_uploaded_file中得到反映。

merged=pd.DataFrame()

def data_prediction(filename):

    model_name = 'SVM.sav'
    SVM = pickle.load(open(model_name, 'rb'))

    df_prediction = pd.read_csv(filename,encoding = "ISO-8859-1")
    df_prediction = df_prediction.applymap(lambda x: x.strip() if isinstance(x, str) else x)

    df_prediction["Description"].fillna(" ", inplace = True) 
    df_prediction['Full description'] = df_prediction['Short Description'] + " " +  df_prediction['Description']
    X_predict = df_prediction['Full description']
    display(X_predict)

    documents_predict = []

    for sen in range(0, len(X_predict)):
        # Remove all the special characters
        document = re.sub(r'\W', ' ', str(X_predict[sen]))

        # remove all single characters
        document = re.sub(r'\s+[a-zA-Z]\s+', ' ', document)

        # Remove single characters from the start
        document = re.sub(r'\^[a-zA-Z]\s+', ' ', document) 

        # Substituting multiple spaces with single space
        document = re.sub(r'\s+', ' ', document, flags=re.I)

        # Removing prefixed 'b'
        document = re.sub(r'^b\s+', '', document)

        # Converting to Lowercase
        document = document.lower()

        documents_predict.append(document)

    data_for_predict = pd.Series(documents_predict)
    predicted_svm_actual_data = SVM.predict(data_for_predict.values.astype('U'))
    output=pd.DataFrame(data={"Description":data_for_predict,"Prediction":predicted_svm_actual_data})

    global merged
    merged = pd.merge(left=df_prediction, left_index=True,right=output, right_index=True,how='inner')
    columns = ['Description_x', 'Description_y']
    merged.drop(columns, inplace=True, axis=1)
    print('HHH')
    print(merged)



    #Provide the name of output file. it will contain the description and predicted output/category
    writer = pd.ExcelWriter(r"predicted_output.xlsx", engine='xlsxwriter')
    merged.to_excel(writer, sheet_name='Sheet1')
    writer.save()
    print(merged)
    return merged



@app.route('/read_file', methods=['GET'])
def read_uploaded_file():
    filename = secure_filename(request.args.get('filename'))
    product = request.args.get("product")

    global merged
    try:
        if filename and allowed_file(filename):
            if(product=='itanywhere'):
                print('itanywhere is happening')
                merged = data_prediction(filename)                
    except IOError:
        pass
    send_from_directory(directory=UPLOAD_FOLDER, filename='predicted_output_new_2.xlsx')

    count_by_prediction = merged.groupby('Prediction').count()[['Incident Number']].sort_values(by=['Incident Number'],\
                                                                                                ascending=False)
    display(merged)
    plt.figure(figsize = (20,8))
    plt.xticks(rotation=90)
    #plt.tight_layout()
    sn.countplot('Prediction', data=merged)
    img = io.BytesIO()  # create the buffer
    plt.savefig(img, format='png',bbox_inches = "tight")  # save figure to the buffer
    img.seek(0)  # rewind your buffer
    plot_data = urllib.parse.quote(base64.b64encode(img.read()).decode()) # base64 encode & URL-escape
    return render_template('data.html',plot_url=plot_data,tables_summary=[count_by_prediction.to_html(classes='data')], titles_summary=count_by_prediction.columns.values,\
                           tables=[merged.to_html(classes='data')], titles=merged.columns.values)




if __name__ == "__main__":
    app.run(host='0.0.0.0')