开发者社区> 问答> 正文

如果else语句在函数中没有正常运行

我有以下功能:

def Graphs(brand,delegation_date,geo):

        plt.figure(figsize = (25,10))

        if delegation_date == '2019-04':
            plt.title(brand + " Apr to Sep 2019 " + geo)
        elif delegation_date == '2019-10':
            plt.title(brand + " Oct 2019 " + geo)
        else:
            plt.title(brand + " Before Apr 2019 " + geo)

        g = temp3.loc[(temp3.Brand == brand) & (temp3.delegation_date == delegation_date),'Green'].tolist()[0]
        y = temp3.loc[(temp3.Brand == brand) & (temp3.delegation_date == delegation_date),'Yellow'].tolist()[0]
        r = temp3.loc[(temp3.Brand == brand) & (temp3.delegation_date == delegation_date),'Red'].tolist()[0]

        gp_grouping = temp3[(temp3['Brand'] == brand) & (temp3['delegation_date'] == delegation_date) & (temp3['geo'] == geo)].groupby(['margin_percentage_floor']).agg({'bid_reference_number':pd.Series.nunique}).reset_index()
        gp_grouping_1 = temp3[(temp3['Brand'] == brand) & (temp3['Sale_Flag'] == '1') & (temp3['delegation_date'] == delegation_date) & (temp3['geo'] == geo)].groupby(['margin_percentage_floor']).agg({'bid_reference_number':pd.Series.nunique}).reset_index()

        plt.plot(gp_grouping['margin_percentage_floor'],gp_grouping['bid_reference_number']) 
        plt.plot(gp_grouping_1['margin_percentage_floor'],gp_grouping_1['bid_reference_number'])
        plt.axvspan(g, maxval, facecolor='g', edgecolor = 'none', alpha=.2)
        plt.axvspan(y, g, facecolor='r', edgecolor='none', alpha=.2)
        plt.axvspan(minval,y, facecolor='purple', edgecolor='none', alpha=.2)
        plt.axvline(float(temp3[(temp3['Brand'] == brand) & (temp3['delegation_date'] == delegation_date)].margin_percentage_floor.median()), color='k', linestyle='--')
        plt.gca().invert_xaxis()

        picture = (brand + "." + str(delegation_date)) + '.png'
        plt.savefig('C:/Users/rvaka/Desktop/Graphs/' + picture, dpi = 300)

我试图通过这个函数运行一个for循环,看起来像这样:

def dplots(geo):
    for i in brands:
        for j in delegation_dates:
                Graphs(i,j,geo)

其中品牌和委托日期是循环遍历的列表。plt。title不断返回else语句(“在2019年4月之前”…),但它不应该只返回那个title。谁能告诉我为什么会这样? 问题来源StackOverflow 地址:/questions/59382115/if-else-statement-in-function-is-not-running-properly

展开
收起
kun坤 2019-12-27 17:18:20 385 0
1 条回答
写回答
取消 提交回答
  • ……

    2019-12-27 17:19:29
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载