对于带有Groupby的循环,错误消息:ValueError:Grouper和axis的长度必须相同

2024-06-10 02:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

运行以下代码时,会收到以下错误消息: ValueError:Grouper和axis的长度必须相同有人能帮我理解为什么会这样吗?我浏览了几个其他相关的帖子,但我仍然不明白为什么我会收到这个错误消息。 Grouper and axis must be same length in Python

for (community,label,lp) in zip(communities,event_labels,landing_pages):
    lp_data_simple.loc[lp_data_simple['Event Label'].str.contains(
            label, regex=True) & lp_data_simple['Landing Page'].str.contains(
            lp, regex=True) ,].groupby([(lp_data_simple.index.year),(lp_data_simple.index.month),
                            'Event Label','Landing Page','Source / Medium','Event Action']).sum()

我有以下三个清单:

communities = ['Firethorne','Glendale Lakes','Kendall Lakes','Kingdom Heights','Lago Mar','Lake Shore','Rodeo Palms',
               'Cypress Oaks','Fall Creek','Plantation Lakes','Sunset Ridge','Townsen Landing','Woodridge Forest']

event_labels = ['firethorne|Firethorne','glendale|Glendale','kendall|Kendall','kingdom|Kingdom','lago|Lago',
                'lake shore|Lake Shore','rodeo|Rodeo','cypress|Cypress','fall|Fall','plantation|Plantation',
                'sunset|Sunset','townsen|Townsen','woodridge|Woodridge']

landing_pages = ['firethorne|not set','glendale|not set','kendall-lakes|not set','kingdom-heights|not set',
                 'lago-mar|not set','lake-shore|not set','rodeo-palms|not set','cypress-oaks|not set',
                 'fall-creek|not set','plantation-lakes|not set','sunset-ridge|not set','townsen-landing|not set',
                 'woodridge-forest|not set']

我有这样一个数据帧:

                            Source / Medium  \
Date                                            
2018-10-16                  (direct) / (none)   
2018-10-16  zillow/paid_network / newhomefeed   
2018-10-17                  (direct) / (none)   
2018-10-17                prospecting / email   
2018-10-17                prospecting / email   

                                                 Landing Page Event Category  \
Date                                                                           
2018-10-16                              /lp-new/sunset-ridge/            KPI   
2018-10-16  /lp/cypress-oaks/zillow/?nhf_channel=6&nhf_lis...            KPI   
2018-10-17                          /lp/woodridge-forest/nhd/            KPI   
2018-10-17  /lp/woodridge-forest/email/?cid=18101714470088...            KPI   
2018-10-17  /lp/woodridge-forest/email/?cid=18101714470088...            KPI   

                  Event Action  \
Date                             
2018-10-16       Click to Call   
2018-10-16       Click to Call   
2018-10-17  Driving Directions   
2018-10-17  Driving Directions   
2018-10-17  Driving Directions   

                                                  Event Label  Total Events  \
Date                                                                          
2018-10-16  Sunset Ridge (Website) - Click to Call (832.22...             1   
2018-10-16  Cypress Oaks (Zillow) - Click to Call (832.220...             3   
2018-10-17  Woodridge Forest (New Homes Directory) - Get D...             1   
2018-10-17  Woodridge Forest (SmartDirectory Email) - Get ...             1   
2018-10-17  Woodridge Forest (SmartDirectory Email) - Get ...             2   

            Unique Events  
Date                       
2018-10-16              1  
2018-10-16              1  
2018-10-17              1  
2018-10-17              1  
2018-10-17              1  

我的最终目标是循环浏览这些列表&提取每个社区所需的相关数据,并将其分组,类似于下面的内容。你知道吗

                                               Total Events  \
Date Date Source / Medium                                Event Action                       
2018 10   (direct) / (none)                              Click to Call                  3   
                                                         Driving Directions             2   
          RDC_NAV / Banner                               Click to Call                  2   
                                                         Driving Directions             5   
          classifiedads.com / referral                   Driving Directions             1   
          google / cpc                                   Click to Call                 10   
                                                         Driving Directions            17   
          l.facebook.com / referral                      Click to Call                  1   
          m.facebook.com / referral                      Click to Call                  3   
                                                         Driving Directions             4   
          msn.com / referral                             Driving Directions             1   
          newhomesdirectory / referral                   Driving Directions             1   
          newhomesdirectory.com / referral               Click to Call                  6   
                                                         Driving Directions            10   
          newhomesource.com / referral                   Click to Call                  3   
                                                         Driving Directions             2   
          prospecting / email                            Click to Call                  1   
                                                         Driving Directions             6   
          yahoo / organic                                Driving Directions             1   
          zillow/paid_network / newhomefeed              Click to Call                  4   
                                                         Driving Directions             2   
     11   (direct) / (none)                              Click to Call                  1   
                                                         Driving Directions            12   
          Hotonhomes.com / Neighborhood Watch Email      Driving Directions             1   
          Hotonhomes.com / Right Time Response Email     Driving Directions             1   
          RDC_NAV / Banner                               Click to Call                  1   
                                                         Driving Directions             2   
          SmartTouch GEO / geotargeting                  Driving Directions             2   
          bing / organic                                 Driving Directions             1   
          google / cpc                                   Click to Call                 51   

更新 我能够通过以下for循环完成任务

for (community,label,lp) in zip(communities,event_labels,landing_pages):
    temp_df = lp_data_simple.loc[lp_data_simple['Event Label'].str.contains(
            label, regex=True) & lp_data_simple['Landing Page'].str.contains(
            lp, regex=True) ,]
    temp_df.groupby([(temp_df.index.year),(temp_df.index.month),'Source / Medium','Event Action']).sum().to_csv(community+'.csv')

Tags: tocomeventdatadatenotcallsimple